Switching between activities without destroying the activity - android

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.

Related

Activity Launch Mode Single Instance Usage

I have two Activities. WebViewActivity and CameraPreviewActivity
I should be able to switch between the two activities on click of a button. Once I have started CameraPreviewActivity, further loads of the activity should not take much time to initialize the camera view. It should be like its getting resumed from previous state.
Is it a good idea to do this by keeping the launch mode of both activities as 'Single Instance' and start the activities when the respective button is clicked?
Is it a good idea to do this by keeping the launch mode of both activities as 'Single Instance' and start the activities when the button is clicked
Yes, mark their LaunchMode as SingleInstance in Manifest, also do not call finish() method in any of them. This will call the activity which is already there in stack.

Going back to the previous activity in android

I go from activity A to B.
Now I want to go back to activity A again.
One way is that i do not finish activity A when I start activity B and then just finish activity B.
Questions:
If android has destroyed the activity A due to less memory or other issues, then that activity would no longer be there in the back-stack and then what happens when we press the back button on Activity B.
How to configure an activity such that if its instance exits, then we go to it other wise we create a new instance?
Thanks and regards,
Sunny
The framework already behaves the way you describe. In scenario 1, the system will restore activity A, even if the app process was killed. Of course, the author of activity A would need to write the appropriate code to save and restore state. As for scenario 2, you can Force this behavior by setting the launchMode field in your <activity> tag in your manifest. See the docs: http://developer.android.com/guide/topics/manifest/activity-element.html

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

Android - Out of Memory Error due to starting a lot of Activities started

I have an application that implements somewhat like having a top page.
So like, i have this activities:
TopActivity , FirstActivity, SecondActivity, ThirdActivity and FourthActivity.
Each activities has a button that when you press it, it will bring you back ti the TopActivity.
The way I implemented this one is every time that button is pressed, I start an activity, a new TopActivity. So, every time a top activity button is pressed, it always create a new activity. If I also will finish() the TopActivity when it goes to first, second and third, I can not go to TopPAge using back button. Are there itger ways to do this?
Any help is greatly apprieciated.
You can use a different Launch Mode for you activities. The launch modes decides when and how to create a new Activity or reuse a previous one.
The singleTask launch mode seems like it would do the trick for you:
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to existing instance through a
call to its onNewIntent() method, rather than creating a new one.

Problem on launching Activity from other application

I have met a problem, hope you can provide some suggestion on it.
Here is the problem:
There is an A Activity in my application which is the application's MAIN Activity, and can be started from other application(like SMS). Consider now there are A-B-C-D activities in my application's task, as I said before, user can start A Activity from SMS, when A is launched from SMS, I want B C D all to be finished, and the instance of A Activity in the task will be brought to front.
I tried to set A activity's launchMode to singleTask, this will do the trick, but there is another problem: for example, I got A-B-C-D Activities in my application's task, user press HOME key to the home screen, and re-launch the application from the launcher, then A Activity is shown instead of D Activity. It's like the application have been started from the beginning, that is not what I want.
Is there any way that I can do this? Any suggestion will be highly appreciated. Thanks in advance!
Haven't tried it, but you can try this:
Set the activity attribute alwaysRetainTaskState to true.
This should take care to retain all the activities in the stack.
When you want the root activity to be shown (In your case : when launched from SMS - I assume you are invoking the activity from your SMS receiver using start activity), in the intent you pass to startActivity set the FLAG_ACTIVITY_CLEAR_TOP flag.
This should clear off since you explicitly mention it in the Intent.

Categories

Resources