Clearing Android activity stack without animation (silently) - android

I have two activities, A and B.
1) From activity A you can go to B and return to A via back button (resume).
2) From activity A you can go to B, make a selection, which will return to activity A via reloading it. Here I need to clear out the previous copy of activity A from the back stack.
In the second case I am using the following intent:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
This works, but my new activity is drawn first, then I can see the old copy getting removed - so there's some animation of the same activity going away. Is there any way to either reverse that behaviour so that the old activity is removed first, or alternatively get rid of that animation? Thanks!
EDIT:
In terms of a workaround, for now I'm setting activity A as a singleTask in manifest, overriding onNewIntent and resetting all my objects that I need to reload in onResume. That seems to be working but I'd prefer a clean slate, i.e. reload the activity

Related

clear all activities except one in android

Below I'm showing the flow on how I want to navigate my Activities:
I tried writing the following code inside D and E:
Intent list = new Intent(AddComplaint.this, B.class);
list.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(list);
However, I'm facing two issues:
When B gets launched it shows a grey screen for a while, then it gets loaded.
When I go back from B it exits out of the application rather than going to A (the dashbord).
How can I fix this?
I believe you can achieve what you want by using FLAG_ACTIVITY_CLEAR_TOP. If you send an Intent using this flag, it will be delivered to the existing Activity B, and any activities above B on the stack (C, D/E) will be finished.
Using FLAG_ACTIVITY_CLEAR_TASK will finish all previous activities on the stack, which would make B the only remaining activity - explaining why you exit the app when clicking back. Your grey background is unrelated to activity management and indicates the activity is simply taking a while to call onCreate().
Example code:
Intent list = new Intent(AddComplaint.this, B.class);
list.setFlags
(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(list);
I think FLAG_ACTIVITY_CLEAR_TASK is clearing all the activities from stack.
to go back from activity B to A again, without changing anything to your existing code , simple overrride onBackPress() method in activity B and startActivity A there.

How to reopen a very same activity / how to keep an activity alive?

I have a main activity and there I start a new activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
To go back to the main activity I used the finish() method.
My question is: How is it possible to switch to the very same second Activity without starting a new one? I thougt the finish() method kills the second Activity, but it still works how the code is telling him (timer counts down and a sound appears) in the background . So is it possible to just switch back to the second Activity on Screen?
In the past I had used a TabWidget which works also with activities, so it it must be possible in some way!? Or do I have to use Fragments? But if I use Fragments I have the same question: How to keep the Fragment alive till I have to use it again?
I hope you can help me with this, that would be great.
I would be very thankful.
You need to use the Intent.FLAG_ACTIVITY_REORDER_TO_FRONT when starting the SecondActivity.class.
It will basically tell Android to search the backstack for an instance of SecondActivity. If such instance is found it'll move it to the top of the stack.
Also keep in mind that if you call finish() on your SecondActivity, the REORDER_TO_FRONT flag practically won't do anything, as you'll never have live instances of the Activity.
If your activity is alive you can resume it by this code:
Intent openMainActivity= new Intent(SomeActivity.this, Main.class));
openMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(openMainActivity);
Or you can clear other activities with this flag:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

How to remove old activities from backstack? (true way)

I'm new to Android development and here's my problem:
I have 2 activities: A and B.
From A, I call B via startActivity(new Intent(A.this, B.class))
From B, I do some things and return to A the same way: startActivity(new Intent(B.this, A.class))
However, when on new-A I press "Back" button, first I see B class and then the old (unchanged version) of A class.
I've tried placing finish(); inside onPause() method. However, then my Activity crashes on orientation change.
How can I properly control activity backstack? Thanks in advance.
When you call startActivity without any additional flags, you will create a new instance of the Activity specified. This means that your back stack ends up looking like this:
A -> B -> A(2)
You have two options:
First option, you can add flags on the Intent that instructs it to reorder A to the front if it is already present in the back stack. Note that this will not close B; you have to call finish() in B afterward.
Intent intent = new Intent(B.this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startAcvitiy(intent);
finish();
Second option is to use startActivityForResult instead of startActivity in A and have Activity B pass a result back to A. I think this is much cleaner and it gives you a very easy way to react to what happened when B was finished (e.g. you can update your UI). See this guide from the Android developers site.
Either StartActivityForResult or think about using fragments. If you need to jump between activities think about why you are doing this? If you are passing info between the two then there are a couple of options. Fragments give you the option and you can hold the data on the Activity that hosts the fragments or you can have a Singleton living in an Application class that you would extend.
Read about activity lifecycle - here. You have to implement saving of instance.
It sounds like that rather than calling startActivity in to go back to A, you can just call instead finish();. This will land you back on your previous Activity.
Alternatively you can accomplish the same if you use FLAG_ACTIVITY_CLEAR_TOP:
Intent intent = new Intent(B.this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Regarding the accepted answer's suggestion to use FLAG_ACTIVITY_REORDER_TO_FRONT - for your case it really doesn't matter. but in a slightly different case where you have A->B->C and you use REORDER to go back to A, you'd actually end up with a stack that looks like B->C->A. With CLEAR_TOP, you'd end up with just A. Point is, REORDER is meant to REORDER, CLEAR_TOP is supposed to pop the backstack. In your simple case they happen to accomplish the same, but in my opinion it's better practice to use the flags as they're intended.

(re)open a specific activity from stack

Is there a possibility to reopen a specific activity from the stack?
So say I open activity a, then b, then c. I do not finish a and b while starting new activities. If a push my back-button without overriding it, I would go to B now of course.
But I want to give a button or maybe the back button to open A, or B independent from its location in the stack. This is kind of achievable by finishing the activities (if I would finish b, and press back button from C, I would go to A). But some of my activities I'd rather not finish.
I researched but could not find how to achieve this. Is this possible?
Of course there would be a check needed if the activity is active, and if it is then reopen it or else open it.
You will obviously have to figure out what logic you want to know which Activity to open but this should achieve what you are looking for
#Override
public void onBackPressed()
{
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, ActivityYouWant.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
Let me know if this isn't what you were looking for.
Flag
the way codeMagic suggested you may produce more Activities in the Activity stack then you need. This may leed to Memory Leaks or other bad behavior.
you should take a look at:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
or even better finish() your Activities.

How can i manage my activities and finish them properly?

I am new in android and I have total 6-7 activities in my application. I want to know how can I manage my activities properly means when I move to the A->B->C->D like that. Then how can I move that the stack of these activities not created.
On moving from one activity to the other I am using the below code:
Intent intent=new Intent(current.this,next.class);
startActivityForResult(intent, 0);
And now if I want to move back on the earlier activity I used the code as:
Intent start = new Intent(current.this,next.class);
startActivity(start);
finishActivity(0);
Is there a special reason that you don't want to use the activity stack and let the activities handle themselves?
The Android system has done a very good job with the activity lifecycle. It allows you to start an Activity from different places without confusing the user because the back button will bring the user back to a different activity.
If you don't have a very good reason to not use the Android guideline try to stick to the way the system is doing it. Every other thing will only give you problems.
You are starting activities for a result but how I understand you you will never return to them.
You can start an Activity and after that just finish the current Activity. That way the activity will not be put on the back stack. Now you need to listen for back button pushes and create the activities that you want to bring the user to.
If you want to move from Activity A to D like going to the start/home screen of you app you do the following:
Intent goBackToA = new Intent(context, StdActivity.class);
goBackToA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goBackToA);
The flag FLAG_ACTIVITY_CLEAR_TOP will tell the system that if the backstack contains an instance of the Activity this activity will be shown and all activity that are between the current activity and the target activity are removed from the backstack. This allows you to go back to a home activity without creating huge loops that the user can move through with the back button.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapear.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapeear or you can press Back button to see the previous Activity .
whenever you want to navigate from one class to another use this code, may be this help you to navigate the Activity,
Intent nextpage = new Intent(CurrentActivity.this,NextActivity.class);
startActivity(nextpage);
this.finish();

Categories

Resources