How to restart an activity - android

I have created an activity. I made the launch mode as singleTask in manifest file. I donot want multiple instance of the same activity should start. I am lunching this activity on button click of another activity.
If i click the button more than once then as i have made the activity as single task then If the activity already running then nothing happens. But I want to relaunch the activity without creating another instance. How to achieve this.
Thanks
Deepak

But I want to relaunch the activity without creating another instance
What you meant my relaunch without creating. I think you may have set of code that you have placed in your onCreate and you want to run when the activity again got control. If so its better for you to place that code to your onResume or onNewIntent. Because onCreate of the single task will only execute once
singleTask
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.

Related

Activities opened with startActivityForResult not appearing in Recent Screen

I'm following this guide to show my activity in Recent Screen like a separate task.
I've added this flag to open my activity in a new task:
newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
When I start an activity using startActivityForResut the new activity is not opened as a separate task.
However, if I start an activity using startActivity it is opening in a new task.
Is this is the default behavior? or am I missing something?
See the documentation for FLAG_ACTIVITY_NEW_TASK:
This flag can not be used when the caller is requesting a result from the activity being launched.
So I guess if you're asking for a result, android forces you to stay in the same task.

How can an activity with 'singleTask' launch mode be not root activity in a task?

This is an image from android documentation:
Activity Y has 'singleTask' launch mode but it is not root activity in the task, that is Activity X. How did it happen?
Upd.:
From the documentation:
"singleTask"
The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.
Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.
Single task only means that this activity can only be created once cf to this website: http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
It doesn't matter if the activity is root or not.
singleTask:
This mode is quite different from standard and singleTop. An Activity
with singleTask launchMode is allowed to have only one instance in the
system (a.k.a. Singleton). If there is an existed Activity instance in
the system, the whole Task hold the instance would be moved to top
while Intent would be delivered through onNewIntent() method.
Otherwise, new Activity would be created and placed in the proper
Task.
I think the answer is that this activity was launched with an intent with FLAG_ACTIVITY_SINGLE_TOP flag, because intents have higher priority than xml tags.

Any way to show an activity after recovering from process termination?

Is there a way to launch an activity when returning to an application after it was terminated to free up memory? Android is attempting to start the previous activity, but some data I need has been cleaned up during termination. If I can push my home activity onto the stack when returning, my app will be able to function properly.
Thanks!
Declare a public static variable somewhere, which is set to true when your main activity has completed its initialization.
In your other activities, in onCreate(), check if this variable is set to true. If not, it means that Android has killed and recreated your process and restarted just the top activity. In this case, just launch the main activity using Intent.FLAG_ACTIVITY_CLEAR_TOP. This will clear the task stack and start your main activity.
This will only work if the main activity is always in the task stack, at the very bottom (root activity). This means that your main activity should NOT call finish() when it launches other activities on top of itself.
Try this:
put an EXTRA value into the intent starting the second activity, anything really, just to indicate it was started from your MainActivity. Then, in the second Activity's onCreate() use getIntent() to obtain the intent that started it. If it contains your extra value, do nothing and let the activity start normally. If it does not, finish() the second Activity and start MainActivity.

Android - redraw UI on back navigaation when using singelTop

To avoid OnDestory() and OnCreate() beeing called all the time when navigating between my main activity and some sub activities, I have set the singleTop option in my manifest. Unfortunatly this causes that the UI in my main activity is gone when returning from a sub activity. Do I really need to redraw my UI manually?
I am still wondering how to handle these basic navigation features. Is it unusual to perform application initialization tasks (e.g. start services) in OnCreate() of the main activity?
I must be missing something from your explanation I guess because the very basic point that you provided is not correct. When you launch another (sub) activity from your activity then your starting activity is not destroyed but it is stopped. So your onStart is called when you navigate back.
So maybe you can explain a little bit more the background of your requirements.
Use android:launchMode="singleTask" for your main activity so the system will not re-create it if you call it via Intent. You can also clear the stack and go back to your main activity by using
Intent intent = new Intent(SubActivity.this, MainActivity.class);
intentHome.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentHome.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentHome);

Destruction of views on returning to activity

I have a MainActivity class, which launches a RaceActivity at some point. If, later, the RaceActivity exits, I want the MainActivity to return, with all its views. However, it seems to be created anew each time. I have implemented the proper section of onOptionsItemSelected, but when I click the back button, I get a new instance each time, or at least the programmatically added views are gone. What can I do to fix this?
Edit for clarification:
I am fine using onCreate with a bundle to restore these views, but I thought that happened automatically if you recreate the same instance of an object. I want to keep programmatically created views when the activity is recreated. Alternatively, I want the activity to stop being destroyed when the user returns to it. (I tested, and it gets destroyed as soon as the use returns.)
Accept my answer if it helps you. it take hours to find the solution..
override onBackPressed()
Intent intent = new Intent(FromAnyActivity.this, CurrentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
and insert the code in it.. It will reorder your Activity from stack and your views and data will remain where it was. There was another way by adding SingleInstance launch mode.. read these link may help..
Add the following to your MainActivity declaration in the AndroidManifest file:
android:launchMode="singleTask"
According to the Android APIs, this addition is for: "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."
If the above doesn't work, I believe you can also try adding this as well to the MainActivity in your AndroidManifest file:
android:alwaysRetainTaskState="true"
According to the Android APIs, this addition is for: "Whether or not the state of the task that the activity is in will always be maintained by the system — "true" if it will be, and "false" if the system is allowed to reset the task to its initial state in certain situations. The default value is "false". This attribute is meaningful only for the root activity of a task; it's ignored for all other activities."

Categories

Resources