Is there a way to totally remove the previous activity?
Explanation:
I have a SplashScreen Activity where I am loading data, etc. and then when it finishes I am navigating to MainActivity like this:
startActivity(Intent(applicationContext, MainActivity::class.java))
finish()
My MainActivity come in foreground, no problem here, but when I am listening to Lifecycle Event with ProcessLifeCycleOwner library, in this particular case:
#OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun appWillEnterBackground() {
Log.e("INACTIVE", "BECOME INACTIVE")
}
Both of my Activities are going on this method while I am on my MainActivity. My SplashScreen Activity should not be able to enter this method because I finish() it.
I tried to return after finish() , I tried "noHistory=true" in Manifest.xml but neither of those worked.
Any suggestions?
You can try finishAffinity() method.
finishAffinity(): Finish this activity as well as all activities immediately below it in
the current task that have the same affinity.
First call finish method and then start new activity.
finish()
startActivity(Intent(applicationContext, MainActivity::class.java));
You can use singletop launch type for MainActivity.
<activity android:name=".SingleTaskActivity"
android:launchMode="singleTask">
Create a broadcast receiver in the activity you want to kill. register it with an intent action like "ACTION_KILL_THIS_ACTIVITY". in onReceive method of this broadcast receiver add finish(); line. When your new activity starts, you are ready to kill the other one so from the new activity's onCreate() method, call
sendBroadcast(new Intent("ACTION_KILL_THIS_ACTIVITY"));
Instead of passing the applicationContext pass the SplashScreenActivity.this as the argument, so your code will look like this:
startActivity(Intent(SplashScreenActivity.this, MainActivity.class));
finish();
Related
Android StartActivity kill the Activity1 first and its ondestroyed should get called. Once this completes then open another activity.
startActivity(new Intent(Activity1.this, Activity2.class));
finish();
even if i call finish first in this scenario Activity1 ondestroy method get call after Activity2 is shown to user. I am looking for proper way where i can be sure that Activity1 get destroyed completely then only show Activity2
You can call finish(), and call startActivity() inside onDestroy():
void onDestroy() {
startActivity(new Intent(Activity1.this, Activity2.class));
super.onDestroy();
}
Mabye noHistory atrribute helps you to get rid of this problem
<activity android:name=".ClassName" android:noHistory="true" ... />
also, take a look at this post :
Start new Activity and finish current one
I have a section of code where I want to go to another activity. After parsing these lines of code, the app doesn't close current activity but keeps executing all the onCreate() method from the current activity.
Intent intent = new Intent(ExtraInformationActivity.this,
MainActivity.class);
startActivity(intent);
finish();
Should the finish() function immediately finish current activity and go to the new activity, or the app should execute all the code from the onCreate method? How can I immediately close current activity? Thanks
Calling finish() in an activity does not INSTANTLY close it, it will close it after it runs all of the lifecycle methods and whatever method you called finish() from.
So if you call finish() in onCreate, it will still call onStart, onResume, onPause, onStop and onDestroy (and all of the regular lifecycle methods).
That is how it works, and you can't change that. So you'll need to adjust your code to it.
to avoid running codes after calling finish, add return; after finish();
Read this
void finish () Call this when your activity is done and should be
closed.
When you are using startActivity(), there is no need to use finish().New activity automatically pops-up.
My project has 4 activities and users from activity A go to B after that to C and D. I need to create a button in activity D to close program directly because if user has to close all activities ( D ->C -> B -> A-> close) it would be unfriendly.
Register a broadcast receiver in each of the activities, listening for the "close all action", when the button in the last activity is pressed, send that broadcast, so all the activities register will execute their "onReceive" method on the broadcastreceiver, and there all them will be finished as long as they are registered.
This will definitely do the trick, although to be honest is quiet a poor implementation, chances that you are doing something wrong in the navigation are high, maybe fragments or a tab would be better suited for what you are trying, in stead of creating such a stack of activities...
Hope this helps...
Regards!
I think onActivityResult could be the better option.You could finish the activity if required task is being completed otherwise just backtrack on previous activity
You should override onBackPressed() from each Activity and call finish().
Assume the first activity in your application is named ActivityMain. Presumably it will be the oldest one on the stack.
Create an intent to start ActivityMain using the flag FLAG_ACTIVITY_CLEAR_TOP. Set an extra in that intent to indicate this is an application exit, and call startActivity() with that intent. This will clear the stack and get you back to Activity main.
In ActivityMain call getIntent() then check for the exiting application Extra value. If it is set, call finish().
A not so elegant solution:
Instead of calling startActivity, call startActivityForResult, from A to D.
On Activity D, when your button is pressed, set any result (let's say Activity.RESULT_OK) and call finish().
On each Activity (from A to C), override the method onActivityResult to check for the result. If the result is Activity.RESULT_OK, then you set the same result and call finish() again.
If you want, instead of just setting the result, add an Intent with some flag to tell the previous Activities to finish themselves.
Simply do one thing . In your activities add an overriden method onPause
onPause(){
finish();
}
This will close all your activities once you press back from any activity.
I have some Activities, let's say Activity A, B and C.
In the Activity A I call the B through a Menu with an onOptionsItemSelected:
Intent main = new Intent (this, MainActivity.class);
this.startActivity(main);
Now, when I'm in the B activity, I can call the A one back in the same way (with Intent and startactivity): how can i handle it to call the OnResume or the OnRestart method of A instead of the OnCreate one?
I'm logging it and anytime I move from an activity to another one, it always call the OnCreate method: what can I do?
Configure your Activity A as "singleTask" or "singleInstance" in the manifest.xml. Then Acitivity A's onResume() will be fire instead of onCreate() when you call Activity A from Activity B (assuming Activity A was already instantiated like you describe). There are drawbacks to this kind of configuration so read this.
example manifest:
<activity android:name=".YourActivityA"
android:configChanges="keyboardHidden|orientation"
android:launchMode="singleTask">
You cannot control whether the OnCreate/OnResume/OnRestart methods are called directly. It is based on whether the previous activity is still in memory or not.
http://developer.android.com/guide/components/activities.html
http://developer.android.com/images/activity_lifecycle.png
I'm logging it and anytime I move from an activity to another one, it always call the OnCreate method: what can I do?
To return to Activity A from Activity B, if you use:
startActivity(), then A's onCreate() will always be called.
finish(), then A's onCreate() may or may not be called. (It depends on whether A was destroyed by you or the OS.)
The first Activity that loads in my application is an initialization activity, and once complete it loads a new Activity. I want to ensure if the user presses 'Back' they go straight to the Launcher, and not the initialization screen. Side note, is this even the best approach, or would this be better done with some kind of Intent Flag?
Is it correct to call finish() after calling startActivity() on the new activity?
onCreate() {
...
startActivity(new Intent(this, NextActivity.class));
finish();
...
}
I'm still taking in the whole 'Message Queue' method of doing things in Android, and my assumption is that calling startActivity() and then finish() from my first Activity's onCreate() will log each respective message in the message queue, but finish execution of onCreate() before moving on to starting the next Activity and finishing my first one. Is this a correct understanding?
Probably you should just use the noHistory flag on the activity in your manifest.xml