android


Revert to MainActivity after pausing/exiting on SecondActivity


I am trying to create an app with several activities.
The starting activity (MainActivity) acts as a directory or the starting page to the link to the other activities.
One of the activity in question (SecondActivity) is the activity where the gameplay essentially takes place. Its limited with a countdowntimer.
My concern is, when in the middle of the SecondActivity (or any other activity apart from MainActivity), if the SecondActivity is paused or stopped for whatever reason, I intend for the app to resume on MainActivity instead of the last Activity it was at (e.g. SecondActivity).
I have tried putting the following inside the OnResume
#Override
protected void onResume() {
Intent revrt = new Intent(SecondActivity.this, MainActivity.class);
revrt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(revrt);
finish();
super.onResume();
}
but the above redirects back to MainActivity immediately after launching SecondActivity. Obviously, I want SecondActivity to play out and do what its suppose to do, only to revert back to MainActivity in the event the player pause/stops (e.g launch another app, phone call etc). The reason for this is that SecondActivity tracks the score for the session and is limited by a predetermined time, and if the player could exit the app and come back to continue where he left off, it becomes a cheat for higher scores.
Is there anyway to automatically revert back to MainActivity upon restart if the SecondActivity is stopped/paused for whatever reason previously?
I think you can declare a Boolean variable which indicates if the secondActivity is launched or not .. if it not launched yet set mIsLaunched = true and if it launched launch the MainActivity
Boolean mIsLaunched = false;
#Override
protected void onResume() {
super.onResume();
if(mIsLaunched){
Intent revrt = new Intent(SecondActivity.this, MainActivity.class);
revrt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(revrt);
finish();
} else {
mIsLaunched = true;
}
}
use this method to save the value before exit
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("IS_LAUNCHED", mIsLaunched);
}
and to get the value again use
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mIsLaunched = savedInstanceState.getBoolean("IS_LAUNCHED");
}
If MainActivity exists always in back stack, you could start SecondActivity as startActivityForResult(intent);
and after catch result in MainActivity (depending on result you can reset it)
Getting a Result from an Activity
As you said you want to call Main Activity when Second Activity goes paused or stopped.
So you should call Main Activity in onStop and onPause method instead of in onResume method. Because onResume method of Second Activity will always called when this activity will come on foreground.
Like...
#Override
protected void onPause() {
Intent revrt = new Intent(SecondActivity.this, MainActivity.class);
revrt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(revrt);
finish();
super.onPause();
}
Hope this help you.

Related Links

how to align the text in CheckedTextView with checkbox
When in Full Screen the layout does not move to y position android
What will happen when System.exit(0) execute?
How to resize image on orientation change..?
Fragment list and gridview in Android Honeycomb
Is it safe to return the instance of service in Binder interface?
Android Reduce or Change Resolution of an Image Selected from Gallery in Android Phone
how to display my gps location from my mobile into map view?
Propagation rules of touch/click events
Is it possible to retrieve and send data from google docs to android app?
Android: How to display JSON Post result
Android - Characters such as å,ä,ö do not render correctly in WebView
Android: Activity is not destroyed using the command 'finishActivity(999)'
How to save bitmap for undo functionality in paint in Android?
How to add limit clause using content provider
Get current notifications android

Categories

HOME
embedded
jpa
string
apiblueprint
package
jsf-2.2
devise
pclxl
edge
ycsb
rtf
openbugs
android-intent
cumulocity
libusb
modbus-tcp
template-engine
identity-management
shortest-path
sse
boost-icl
drive
jboss6.x
data.stackexchange.com
genexus-sd
tizen-tv
twiml
kudu
record
angularjs-material
mnist
roblox
parse-tree
attributeerror
oracle-service-bus
wc
stylecop
uiimage
oim
reportservice2010
tinymce-3
qtranslate
wpas
context-sensitive-grammar
excel-dna
flac
vibrate
donations
async.js
uicolor
signalr.client
refinerycms
swizzling
mobiledoc-kit
tfs-workitem
forwarding
morton-number
cgi-bin
canalyzer
zscript
viola-jones
vacuum
diff3
delayed-execution
entry-point
paw
ignite-ui
personalization
spring-remoting
mt4j
gd-graph
observium
abstract-data-type
robotlegs
cuda-gdb
thttpd
josql
koken
kqueue
catransform3d
u2netdk
update-statement
creole
facebook-iframe
django-piston
build-environment
skype4java
dojo-dnd
pbcopy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App