Lollipop transitions not working when resuming activity - android

I have been trying to implement the new Lollipop activity and shared elements transitions during a few days following the steps from Alex Lockwood awesome blog posts.
But now I'm facing a bit of a problem.
My application uses a DrawerLayout for navigation, but I can also launch some of the activities when clicking other views and buttons. I have set correctly all the Enter, Reenter, Return and Exit transitions for all the activities as well as calling:
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(activity, getSharedViewsPairs(activity)).toBundle());
for the shared elements between those activites.
If I launch the activities for the first time, the transitions work up well. My problem comes when I try to launch/resume an activity that has already been called before but that now is paused and lives still on the activity stack. When I try to bring this activity to the foreground, then there is no transition.
I also have to say that I set to all my activities the Intent flag FLAG_ACTIVITY_REORDER_TO_FRONT so that in case they have been already launched before, I don't fresh launch them again. Could this have anything to do with it? Am I missing some method that needs to be called when resuming activities that have been launched previously with transitions?
Code for preparing the activity transitions:
public static void requestTransitionsAnimations(Activity activity) {
if(MaterialAnimations.isAnimationSupported()){
Transition transition = TransitionInflater.from(activity).inflateTransition(R.transition.material_transitions);
activity.getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
activity.getWindow().setAllowEnterTransitionOverlap(true);
activity.getWindow().setAllowReturnTransitionOverlap(true);
activity.getWindow().setEnterTransition(new Explode());
activity.getWindow().setReenterTransition(new Explode());
activity.getWindow().setReturnTransition(new Explode());
activity.getWindow().setExitTransition(new Explode());
activity.getWindow().setSharedElementsUseOverlay(true);
activity.getWindow().setSharedElementEnterTransition(transition);
activity.getWindow().setSharedElementReenterTransition(transition);
activity.getWindow().setSharedElementExitTransition(transition);
activity.getWindow().setSharedElementReturnTransition(transition);
}
}
as

Related

How to close/finish the first activity when navigating from first activity to second activity using android navigation component

I have been using android navigation component for the first time and I am new to the Android architecture component as well. As far as I have seen, the navigation component in android architecture uses fragment predominantly for navigation within the same activity. I was trying to navigate from one activity to another using the navigation component:
activity.finish()
Navigation.findNavController(btnView).navigate(R.id.activity)
Here R.id.activity is the id of the activity defined in the navigation graph XML file.
When I press the back button, I was still able to see the previous screen. My question is how the back stack works in the navigation component and why my first activity appears even though the destroy method of the activity is called (due to activity.finish()) ?
I can answer your second part for the first part someone will better understanding will explain you.
Your activity is not finished by executing this below method because if it was finished then you don't see it on back stack.
activity.finish()
finish current activity after navigate method. Below will be the correct way finishing current activity.
btn.setOnClickListener{
Navigation.findNavController(btnView).navigate(R.id.secondActivity)
(activity as currentActivity).finish()
}

Using startActivityForResult from activity which was launched with FLAG_ACTIVITY_NO_HISTORY

My app consists of a service (which implements a state machine, responding to UI and network events) and several state activities, corresponding to the states of the state machine.
The state change uses FLAG_ACTIVITY_NO_HISTORY to launch state activities, so that the user cannot bypass the state machine and get "back" to the "wrong" activity. This makes the "back" button exit the app. (The state persists when the app is relaunched.)
I want to use startActivityForResult to launch a non-state activity.
When the non-state activity is launched, the parent state activity is stopped, as I would expect.
When the non-state activity calls finish(), the parent state activity is destroyed, rather than being (re)started, even before the parent's onActivityResult is called.
Is this because the parent activity was launched with FLAG_ACTIVITY_NO_HISTORY?
Yes, I found that this was the cause of the issue.
I meet the same problem, and Adeel Turk solved it! thanks to you both!
Occured when switch out/home pressed/call StartActivityForResult, app will get "killed"(finished).
just add this to the second Activity.(MainActivity)
btw, my first one is startActivity(with pattern lock). second is MainActivity()
#Override
public void onBackPressed(){
FragmentManager manager = getSupportFragmentManager();
if (manager.getBackStackEntryCount() > 1){
super.onBackPressed();
}
else{
finish();
}
}

Android Lollipop home button return animation

I'm trying the new Android API, specifically the new animations. I have two activities and used setEnterTransition() and setExitTransition() on the second activity with a Slide transition. Everything works fine when I switch activities using the buttons inside them, or using the back button, but when I'm on the second activity and I press the Home Button the return animation is not played...
Second activity onCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getActionBar() != null) {
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Slide slideTransition = new Slide();
slideTransition.setDuration(1000);
slideTransition.setSlideEdge(Gravity.RIGHT);
getWindow().setEnterTransition(getSlideTransition(Gravity.RIGHT, 1000));
getWindow().setExitTransition(getSlideTransition(Gravity.LEFT, 1000));
getWindow().setAllowEnterTransitionOverlap(true);
setContentView(R.layout.activity_second);
//...
}
The first activity is set as parent of the second activity in the manifest.
I find some solutions that use the overridePendingTransition() method, but I would like a solution that uses the new methods (if that is possible).
Thanks!
This is by design. The return transition is only triggered when the activity is explicitly finished (i.e. you press the back button or call finishAfterTransition()). When you press the home button, you are putting the application into the background so that the user can return to that same activity at a later time. If you were to finish the activity when you pressed the home button, the user would be confused as to why they were not taken to that same activity when they return to the application later on.
It is also worth mentioning that the new Lollipop transition APIs are not meant to replace overridePendingTransition(). The two are fundamentally different. The Lollipop transition APIs give you a way to animate the contents inside an activity's view hierarchy individually when you switch from one activity to another. On the other hand, overridePendingTranition() allows you to override the system's default window animation when the activity window is being added or removed from the screen. In other words, the former operates on the views inside the activity's window whereas the latter operates on the entire activity window itself.
One last major difference between the two is that the new Lollipop transition APIs only work between two activities that belong to the same task. If you want to perform an exit/enter animation when navigating between two activities belonging to two different tasks, you'd need to use overridePendingTransition() instead.

Managing activities in Android and preventing large activity stack

I'm new to Android development and I have an app with various activities. For performance reasons I'd like to properly manage the activities when users are using my app. Here's my problem.
Activity A - starting activity with map
Activity B - user navigates to Activity B (a list view) from Activity A.
The user then selects the map icon to navigate to Activity A again.
So if you can image it, my activity stack is now:
Activity A
Activity B
Activity A
So if I press the back button the device it will take forever as it scrolls through activities.
Is there a way of managing this so the old activity is destroyed and is just re-created upon choosing an activity nav icon. I've read up about onDestroy() and onStop() but I'm a little confused at their implementation.
Apologies for a poorly worded question but I'm unsure of the correct lexicon to ask about activities.
One simple solution is to kill the Activities as soon as they leave the foreground.
You could do that by calling finish() inside onPause().
You could have B finish itself and return to A instead of starting another A. Or, if your stack might be more complicated, like this:
D
C
B
A
D could start A with FLAG_ACTIVITY_CLEAR_TOP, which would cause D, C, and B to be finished, leaving A on top. (That intent flag interacts non-intuitively with a couple other flags, so read the docs.)

ANDROID: Activity state after pressing the back button

Imagine you have the following sequence of activities:
Activity A -> Activity B -> Activity C
When you are on Activity C, pressing the native back button, takes you to Activity B. Now what is the state of Activity C? Is it still in memory or it has been finished?
If it is still in the memory, is there a way to resume the activity? Other than starting another instance of this activity...
I should add that this is the standard case where you do not use any flags including: FLAG_ACTIVITY_CLEAR_TOP
Default behavior is that when you press hardware "back" button, current activity will be removed from the backstack and activity "destroy" sequence will be initiated. From that moment you should not rely on the fact that it might be somewhere around - it is all up to Android to decide when does it actually kill this activity.
What my previous investigations show is that victim's onDestroy() will be called only when new activity is done loading and is idle.
You can specify android:launchMode="singleInstance" for your activity in Manifest. This will ensure that only one instance of activity is created at the time
You might want to consider reading the official docs.
More specifically the part that answers your question:
When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).
Now for your second question… you can keep reading the same page…
when you start an activity, you want to bring forward an existing instance of it (instead of creating a new instance on top of the back stack)
So if you read that… you will find…
You can do these things and more, with attributes in the
manifest element and with flags in the intent that you pass to
startActivity().
In this regard, the principal attributes you can use are:
taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch
And the principal intent flags you can use are:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
No, it is not in memory. It gets finished when you press the back button. You should use android:launchMode="singleTask" in the androidmanifest.xml for that particular activity for which you want no new instances to be created if an instance is already there. For further information this link will be helpful.
In the following sequence...
Activity A -> Activity B -> Activity C
When you will press back button in Activity C then onBackPressed() method will be called. The default behavior of onBackPressed() is to finish the current activity until you Override the functionality.
So, in normal case, after back press the current Activity will be destroyed and you can't find it in the Activity Stack, so you can't resume it.
You will find more information from Android Developer's doc in below link...
Tasks and Back Stack

Categories

Resources