Activity Launch Mode Single Instance Usage - android

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.

Related

Switching between activities without destroying the activity

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.

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 : launch mode for activities

I have multiple activities in my application .
The flow for activities is
A->B->C...
A->D->E...
A->F->G..
....
And each has a back button to go back to previous activity and home to go back to A.
My question is when i launch each of these as singletask, i can visible feel fast switching between activities when i click back or home. I dont see any issues if i keep them as singletask.
BUt are there any issues that i am verlooking if i keep the activities as singletask in my application. Thanks a lot for your time and help
Yes there are issues in changing the behaviour mode of activity from mainfest.xml, When you will press the home button for android home screen and get back to the application the previous activity will be gone and will finish the application, then better is to use intent instead using single task mode, you can use flags with intent to cleat the activity stacks..and by launching the mode in single task you are changing the systems settings, why dont you keep the launch mode and give it to system for handling the activities..!
Intent intent = new Intent(getApplicationContext(),A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
A "standard" or "singleTop" activity can be instantiated many
times. They can belong to multiple tasks, and a given task can
have multiple instances of the same activity.
 In contrast, "singleTask" and "singleInstance" activities are
limited to just one instance. Since these activities are at the
root of a task, this limitation means that there is never more
than a single instance of the task on the device at one time.
The clearTaskOnLaunch attribute If this attribute is set to
"true" in the root activity of a task, the stack is cleared down
to the root activity whenever the user leaves the task and
returns to it. In other words, it's the polar opposite of
alwaysRetainTaskState(which u can use if u want to retain all activities in a stsck). The user always returns to the task in
its initial state, even after a momentary absence or even after pressing home in ur case.

android, starting and exiting activities

I have not really understood the handling of activities and the stack.
I have 3 activities, A - a splashcreen, B- a menu and C another Activity. I start the splash and exits it after a while when the menu is started.
In code I handle them all like this:
startActivity(new Intent(this, ContactInfoMenu.class));
finish();
Now, if I start the app and goes A-B-C, when I hit "Back" in C screen I jump back to B-the menu. Another "Back" exits the application, just like I want.
BUT .. if I go A-B-C-B-C - the "Back" button in C screen exits the whole app instead of getting me back to the B screen?
Why is that? It does like that in all my "subscreens", I can only enter them once, if I enter them a second time the "Back" button exits the app. And I have not tried to catch the "Back" action anywhere? Shouldn't I always call "finish()" when I start a new activity?
Regards
Finish is good for leaving the current activity and going back to the previous one. Otherwise, try to avoid calling finish() if you can help it.
There are a set of flags that you can pass when you start an activity that do a better job of determining how that activity behaves on the stack. These include:
FLAG_ACTIVITY_NO_HISTORY - your activity will not remain on the stack after another activity covers it.
FLAG_ACTIVITY_CLEAR_TOP - a good way to pop off a bunch of activities when you need to "go back" to a certain activity.
Many of these flags can be set in the manifest. Reading up on them will give you a better idea about "The Android Way".
Basically, You don't need to call finish() every time you go to another activity. If system is low on memory it will close your activity instance by itself.
finish() is more often used when yor are inserting some information in one page and then moving on to some other page. In that case, you might need to fininsh your first activity.
But in case where you need to shuffle between views, you must not use a finish() function, because it will cause the whole application to finish.
Try using back button of your own in your views to shift between activities, where you can move to any other activity of your application or even to the Main Screen.

Categories

Resources