Android: Launch new instance of previous activity then finish - android

In my program I start with activity A, then launch activity B from there. However, I then want to launch another instance of activity A on top, and when the user clicks back, I want it to take them to the first instance of Activity A. So I tried launching a new instance of activity A then calling finish(), but it ends up never launching the new activity and just finishing activity B, taking me back the first instance of activity A.
This all works fine when all three activities are different, but when the first and last ones are the same, that is where the problem appears.
Also, I checked and the launch mode for activity A is "standard". I am able to directly launch a new instance of activity A from itself.

So I tried launching a new instance of activity A then calling finish(), but it ends up never launching the new activity and just finishing activity B
Then your code sounds broken elsewhere. Calling startActivity() and then finish() would work as expected

Related

Keep instance of Home Activity when launched

So, my app has 5 activities: A, B, C, D, E. A is the splash (launcher) activity and never gets called again after launch. After launching, B gets called and serves as the Home activity. B, C, D, E all get called with Intent.FLAG_ACTIVITY_REORDER_TO_FRONT so that one instance is reused and updated through onNewIntent(). It works. I'm happy with the user experience.
Here's my question. I would like the first instance of B (upon launch) to hold as the root task. In other words, I would like this first instance of B to be the last screen the user sees if pressing the back button continually until exiting the app. All other of instances of B should be "recycled" normally using the Intent.FLAG_ACTIVITY_REORDER_TO_FRONT.
I'm pretty certain I can make a unique activity (call it B2) and accomplish this, is there a better way?
You can use
Intent cActivity = new Intent(BActivity.this,CActivity.class);
cActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(cActivity);
setting Intent.FLAG_ACTIVITY_NEW_TASK will hold your B Activity's instance and open your Activity C as new task. or you can call startActivity() without setting any flag.

Switching between activities without destroying the activity

I am new to android so don't know what can be the solution to this problem.
I have created an app with two activities in it named these as first and second activity.
First activity launch mode is singleTask and second activity launch mode is standard.
Now when i switch from second activity to first activity i don't want to destroy the second activity.
But in this case it is getting destroyed.
So can anyone help me in suggesting how can i achieve this scenerio of switching from second activity to first without
destroying second activity.
This is because activity one is single task activity.
Read here. According to this
"singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.
To avoid this scenario, change launch mode of activity one to standard.
No need to destroy any activity for this, i think so. Just implement this:
Intent intent=new Intent(ActivityOne.this,ActivityTwo.class);
startActvity(intent);
in your oncreate() method of actvity
You can open new activity that you want to open, without finish() your current / previous activity by using finish().
Once you open multiple activity without finish() previous ones, the OS will automatically maintain the activity stack.

Launch existing singleTask Activity from launcher icon

Scenario:
Activity A (MAIN and LAUNCHER in manifest) starts up when clicking on launcher icon.
In turn it launches Activity B.
Activity B then launches our main app Activity C (MAIN and singleTask in manifest).
Behaviour I require:
Once Activity C has been shown and the home key is then pressed, the next time the launcher icon is pressed I would like to skip straight to Activity C (and not show Activity A (and consequently B) again).
I have tried using FLAG_ACTIVITY_CLEAR_TOP from A, but I still get Activity A whenever I hit icon on launch screen.
Is appearance of my singletask Activity C from launcher achievable?
Update: Use of FLAG_ACTIVITY_CLEAR_TOP from A and not calling finish() creates the situation whereby Activity B appears on press of launcher icon. However, also applying use of FLAG_ACTIVITY_CLEAR_TOP from B and not calling finish() does not resolve situation. now I don't get A on launcher icon press, but get B. What gives!
See similar scenario here.
In your case, I would recommend using a similar approach, where you would have a SharedPreference that would persist a value representing whether your app had previously been launched.
Create a simple Activity that will contain the logic for what you are trying to do here. In that activity's ("pre-A" Activity) onResume() method, check the value of the preference representing whether the app has ran previously.
If it has not previously been ran, launch an intent to Activity A, which calls the subsequent B and C activities; otherwise, launch an intent to Activity C.
Seems pretty straightforward, just remember to define the new "Pre-A" activity in your manifest!

destroying the old instance of an activity in another task when a new instance is created in new task

i have 3 activities A,B and C. The flow is like this:
A->B->Home->C->B.
I have used singleTask on activity B.
However this creates a side effect that when i do A->B->Home->recent, it returns to A instead of B. Activity B must be brought to foreground.
Also is there any way that i can destroy old B when new B is created??
You could call finish() in your onPause callback. But you should handle rotations else your app will be exited on that event.

Android activity lifecycle - activity restarts when coming from history

I have a splash activity (A) that calls a listview activity (B) which calls another activity (C).
When I'm on activity C and I press Home, than kill the app (or wait of Android to do it), than longpress Home and come back to activity C there's a strange problem:
When I click back I go back to B. Than I have a backbutton handler that asks the user if they want to exit and calls finish() on the activity. When I try to exit in this scenario, activity A starts again.
On regular operation it finishes B and doesn't go back to A.
Why is that??
Thanks
When the app is killed (either by you or by Android) the process hosting your applications is killed. However, Android remembers the state of the activity stack (in your case A->B->C).
When the user returns to the app, Android creates a new process for the app and recreates only the activity that was at the top of the activity stack (in this case: C). Now the user presses BACK, which causes activity C to finish and Android recreates the instance of activity B which is then shown (You will see calls to B.onCreate(), B.onStart() and B.onResume()).
Now the user presses BACK again. Your back button handler tries to call finish() on activity A, but there is no instance of activity A. Android hasn't created it yet! When activity B finishes Android remembers that there was an instance of activity A in the activity stack underneath B so it recreates the instance of activity A which is then shown (You will see calls to A.onCreate(), A.onStart() and A.onResume()).
I hope this explains what you are seeing.
Make sure you are calling finish() on A when you load B

Categories

Resources