Top-level Android Activities - android

I'm trying to create a basic app using the AppCompat Drawer, and multiple top-level activities (Not fragments) - I can't quite work out how to manage the backstack - I've tried about a hundred different approaches - but they all require some sort of weird hack to either clear the backstack - or finish the existing activity.
I have 3 activities - A, B & C. A & B are top-level, C is a child of B
I want:
To start Activity A when the app starts
To exit the app when I press the back button from A
To start Activity B from the drawer
To exit the app when I press the back button from B
To start Activity C from Activity B
To go back to Activity B when I press the back button from C
When I start B from B or A from A or B from C by selecting drawer buttons - I should get the top-level Activity back in it's vanilla state.
I have:
protected void startActivity(Class activity) {
final Intent intent = new Intent(this, activity);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
overridePendingTransition(0, 0);
}
Basically I pass in either ActivityA.class or ActivityB.class - with this approach - pressing back from B takes me to A
Using HO_HISTORY, looks ok - but pressing back from C exits the app
Using REORDER_TO_FRONT doesn't seem to do anything??
Using finish() after startActivity works perfectly - unless you choose A or B twice (in which case you exit the app)
Using FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK works perfectly in every manner - except for the super nasty screen flashing as tasks are re-created. And the performance hit...
Help??

Can't you just call finish() right after your starActivity call? (you'd have to remove the SINGLE_TOP flag too -- or you'll run into the behaviour you mentioned when going from B -> B)

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.

Android - onBackPressed to turn off app

hope you are having a great day!
So today I've got a rather simple question. In my main activity when I press the back button I want the application I've developed to turn off no matter what it did right before. And delete the stacks so it opens up fresh the next time.
The activities work like this:
A (Main Menu)
There are 4 buttons here, leading to 4 different activities B (About), C (Change Theme), D (Calculate BMI with US Measurements), E (Calculate BMI with EU Measurements)
D and E both lead to F (Results).
When I am in F and press back I want to get back to the previous (D or E) - which is fixed and it keeps the last typed in information for convenience.
On the F activity there's a button to go straight back to the menu. And if I press this button and then the standard android back button I will get from A to D/E and then to A again before closing out the application.
When I am in B, C, D, E I want to go back to A.
When I am in A I want to turn off the application.
The settings for each one of them is:
A - onBackPressed() - Here I want to put to turn off the application.
onClickA/B/C/D()
{
Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent);
finish();
}
B - android:noHistory(true)
onBackPressed()
{
Start New Intent A.
}
C - android:noHistory(true)
onBackPressed()
{
Start New Intent A (so the changes take effect on the Main Menu even if you press back).
}
D - onBackPressed()
{
Start New Intent A
finish();
}
E - onBackPressed()
{
Start New Intent A
finish();
}
F - Nothing specific.
You can use System.exit(0) method onBackPress()
Well first of all you might want to switch to fragments because what you are doing here doesn't seem so complext that an activity would be needed here.
If you switched to fragments it's easy to add those fragments to the backstack that you want to come back from.
That way you have to overwrite the onBackPressed() only once.

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

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?

android activity always on top

There are 2 activities: A and B
if activity A is started then it will be always on top of activity B (if B exist)
if B is started and A is in foreground then B will go right under A
if A is going in background or is closed then(if B exist) B will be shown
each activity is running in its own task
In other words is there a simple way to start an activity right under the current activity so when that close then it will be shown
what is the simplest way to do this using manifest flags preferably
Start A inside new Task stack. Read docs for Intent.
Inside onCreate of A check Intent and open B.
Now you have new stack where B is above A, however and A and B were used before.

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();

Categories

Resources