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

I want to know is it possible and will it be implemented to record calls on Android 7?
Testing rotation/orientation changes and cannot find what is causing this error [duplicate]
can we directly passe the mobile number to digits
Android: Simulating power button press using intent in terminal simulator
Firebase data not added to the right child
How to change navigation menu to back arrow?
Android - When to write to database, to prevent writing extensively
Execution failed for task ':app:transformClassesWithDexFor官方Debug'
Dagger 2 Scoped Activity Context Issue
How to save List in onDestroyView in Fragment?
upload data to server using json android
How to use AsyncTask?
Android actionbar won't show title or settings
Android - How to use Realm in FirebaseMessagingService class (non activity)
Display Name of Files In Progress Bar While Syncing
How change Searchable Spinner list background color in android?

Categories

HOME
spark-streaming
apache-flink
github-for-windows
rsa
requirements
tivoli
cakephp-3.4
maxscript
jquery-ui
angularfire2
crystal-lang
flume
pega
why3
redis-sentinel
datastage
gruntfile
microsoft-dynamics-nav
mapstruct
r-mice
php-mysqlidb
licensing
angular-translate
ntp
constexpr
aws-codecommit
sql-server-2000
glib
gojs
classcastexception
dynamic-jasper
mnist
salesforce-communities
graphhopper
dbnull
subclipse
getchar
ovf
dotnet-httpclient
yii2-api
bluez
semantic-logging
ngmaterial
boo
reshape
gpg-signature
choice
simplepie
segment-io
tinkerpop3
delta
boost-serialization
createprocess
qbxml
highland.js
jbase
character-replacement
terracotta
mod-proxy
ocra
cwrsync
user-permissions
xmltype
code-documentation
apache-spark-1.3
browserify-shim
sonarqube-5.0
post-commit-hook
with-statement
kissfft
ora-06550
c++builder-xe5
wimax
pstack
uimanageddocument
lambdaj
promotion-code
vs-android
rowlex
aspmenu
scrollbars
nstokenfield
skype4java
wcf-instancing
wcf-proxy

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