Android How to put activity in the background but not in stack - android

I have activity A and activity B. When app opens activity A opens.
I go to A to B. Then how do you "go back to A from B", without destroying B so that when you go from A to B the process is super fast? (Also how would you have to open old B and not create a new B?)
Also one more question, when you "go back to A from B", is there a way you prevent B to show up when you press back button once you are back in A?

When creating B, do all the hard work in onCreate, so later when resuming it, it starts up faster. B is destroyed only when it's not used and android needs to free some memory. Since it's in the background, switching to it will be pretty fast. To speed it up you can switch without an animation:
Intent intent = new Intent(this, B.class);
startActivity(intent);
overridePendingTransition(0, 0);
To prevent multiple B activities to be created, in your manifest's B activity tags set:
android:launchMode="singleTask"
After you go back from B to A, going back again, will quit the app. That is unless you go to A explicitly. Then you will want to override onBackPressed() in activity A, that always quits the app instead of going back to B.

Think you need to read this: http://developer.android.com/training/implementing-navigation/ancestral.html
If you want to always exit your app when pressing back in A, then you can override 'onBackPressed'.
Regarding how you would keep A 'alive' so to speak, you could set A to be 'singleInstance' or 'singleTask' in your manifest Android singleTask or singleInstance launch mode?

Related

clear/finish all activities including SingleInstance activities from the stack in android

I have three activities namely A, B, and C. A is launcher activity while B and C are singleInstance activities. The sequence of movement between activities I want is A(press button) -> B and then B <-> C (i.e, B and C keeps alternating as many times as they want). When in B, pressing navigation up button takes me to activity A.
Now the problem arises. I am in A now and want to go to B(by pressing button) like I did during start of my App(i.e, I want it to work as if the app has been launched). But it tries to go to activity B and suddenly with a flash it returns back to A. The funny part now is that when I try again(i.e in activity A I press the button again) it goes to B without any problem and the flow works as expected i.e, the flow of activities are same as it was on launch time.
What I want finally?
Once I reach launcher activity which is activity A, I want the app to behave as if it was just launched. i.e all activity including singleInstance activities should be cleared from the stack.
I have tried this in onSupportNavigationUp() method in activity B:
Intent mainIntent = new Intent(B.this, A.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
B.this.finish();
I even tried adding Intent.FLAG_ACTIVITY_CLEAR_TOP also.
But this doesn't help. The problem remains unsolved.
Any kind of suggestion will be greatly appreciated.

How to go back to Home Activity from a specific activity without overriding back button

I am new to Android . Here I have for activities A,B,C,D in which A is the Home Activity.It is in stack as A->B->C->D
When I press back from B or C it should go back just as normal. But if I press back from D it should go back to A and from A the app should exit
I guess you could intercept onStop() and guessing if the activity is switching to C and launching A instead. But it would result in a hard to maintain mess and I do not recommend that.
However, if for some reason you still not want to override onBackPressed and you manage to guess that D is stopping because back was pressed (without overriding onBackPressed(), just start A activity from there with an Intent with FLAG_ACTIVITY_CLEAR_TOP (call i.setFlags(FLAG_ACTIVITY_CLEAR_TOP) )
According to the doc:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
So A will be brought back and B and C will be cleared.
when you start your new activity finish the old activity for example if you want to start C from B :
Intent I = new Intent(B.this , C.class) ;
startActivity (I);
B.this.finish();

Android reuse activity from activity stack and clear it

I have application with main activity and some more.
On each other activity there is the logo of the application.
When user presses the logo button, I want to get back to the main activity.
I do not want to create new intent, since activity aready on the activity stack.
How can I get it - use the one on the stack?
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Yoav
I do not want to create new intent, since activity aready on the activity stack.
If you start an activity (via intents or any other way) which was already started and is on the stack , then Android just takes that same instance of the activity and places it on top of the stack. A new instance is not created. Ofcourse this happens if you did not manually kill the activity (by calling finish() in it).
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Its not recommended to override the back button to quit the application in every activity(Unless your app has strong reasons to do so). generally the app should let the user go back to the previous activity when he presses the back button (which is what a user might be expecting).
If you still would like to quit with the back button then you can override the back button function and launch the intent that leads to the home screen:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I faced a somewhat similar problem. The following link might be helpful for you:
clearing stack of activities with just one press
Its very simple.
Don't call finish() on Home/Main activity.
For Ex: Say you have 4 activities.. If your requirement is like this .. Act1-->Act2-->Act3-->Act4-->Act1 . So, don't call finish() on Act1. But call finish() on Act2, Act3 while you are going to other activity. So when you click on logo in Act4, just call finish(). So, automatically you will come back to Act1 which is your Main activity.
If you have logo in Act2, Act3 also then call finish() on click of logo to go back to Main. But remember to call finish() on Act2 while you are going from Act2 to Act3

Android - login and relogin

in my activity I have login page (L), which leads to hierarchy of activites (L -> A -> B -> C). When user log in, and he goes up to activity C, he minimizes his app and after some while, system will do a force close on this app.
Now, when he start this app again a he log-in, he should have see, where he ware last time an application was running with all opened activities on stack (if he was in C, one back button leads to B, then to A, then to L). How to achive such a behavior in Android? I am now using sharedpreference, which is hodling string of visited activities, then some flag, which tells me whether an app was finished with System close or user close and them I am persisting each activity with its own sharedpreference. If system kills my app, after login I open series of past opened activities in For cycle, but they are on the stack only. They are opened (= onCreate method is run) only when I use back button to see them.
Do you see any cleaner approach?
Thanks
When I want to maintain hierarchy after a force close I bypass the activity stack by replacing each activity with the one that it calls and then override the back event to do the same in reverse.
In ActivityL.java, where you want to go to ActivityA:
Intent intent = new Intent(getApplicationContext(), ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
You now just have one activity on the stack - ActivityA. To have the back button behave properly and return you to ActivityL, add this to ActivityA:
#Override
public void onBackPressed () {
Intent intent = new Intent(getApplicationContext(), ActivityL.class)
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
You'll still need to store enough state information to get you back to the activity where the user was before the force close. In onCreate for ActivityL, check that state info to determine which view you want the user to be on and use code similar to the first block above to go directly there.
This seems a bit cleaner to me than rebuilding the entire activity stack on startup. This does however become more complex if your activities don't follow a strict hierarchy. (i.e. sometimes activity A starts activity B and sometimes it starts activity C).

How do I clear all Activities from the stack?

I am having trouble popping all activities off the stack using Intent.FLAG_ACTIVITY_CLEAR_TOP and android:launchMode="singleInstance".
In my application activity A, launches activity B (via startActivity) which in turn launches activity C (via startActivity). On activity C the user presses a menu item to return to activity A. When they arrive at activity A, I want only A on the stack such that if they click the back button they return to the home screen (desktop).
This is the code that I am currently using when the user presses a button to return to A:
Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
All activities are defined as android:launchMode="singleInstance" in the project manifest.
My code doesn't seem to work though. Once I'm back on activity A if I click the back button I return to activity C. Am I misunderstanding how to use Intent.FLAG_ACTIVITY_CLEAR_TOP?
I've always found the best way to ensure C would be removed from the stack is to call finish() after startActivity to remove C from the stack.
The documentation does read as though things would behave the way you expected them to, but it would seem this isn't happening, so finish() will ensure C is removed.
I usually use the technique Al suggested (calling finish() after starting the new activity).
You could also experiment with task affinity. I've never done that myself, but it may be relevant in your case as well. See this thread: http://groups.google.com/group/android-developers/browse_frm/thread/ca3b26a14d024597/129e37375105901b

Categories

Resources