REstart activity from Service - android

I can start activity from service but I cannot restart it, this flag only brings to front the existing activity:
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
how could I restart (finish the old, start new)?

It's bit a trickery but what you can do is to add something like RESTART=true to the intent that calls the Activity. In activity's onResume you can do getIntent and see if that flag exists and if so call finish() and call activity anew

Try finishing the activity first by calling:
activity.finish();
then do a new intent to start the activity again:
myIntent = new Intent(context, activity.class);
startActivity(myIntent);

Related

how to start A activity after finish old A activity

I tried to set intent.setFlags (FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK), but the effect is not as I think.
It will execute onResume () in the old A activity.
But I want the effect as shown below:
A activity onCreate() ... onPause()
anywhere(from notification, from B activity ...) start A activity again
old A activity onDestory()
create new A activity
you should use FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
SAMPLE CODE
Intent i = new Intent(YourActivity.this, OTHERACTIVITY.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
use (FLAG_ACTIVITY_CLEAR_TOP) flag when u passing intent. then after startactivity() use finish() method.

Closing an android application programatically in android

I have a application that has two activities
Activity1
Activity2
Now i start Activity1 and move to Activity2 now i want to close the application at a moment
If i use finish() i am able to close only Activity2 and not
Activity1
What i want to do ::
i want my application to quit(close all activities and go to
homescreen)
But i don't want my apk to be removed from android system itself
How can i achieve this ?
{Edit}
I used the code(Activity2 has a fragment which launched a dialog on
"ok" condition in positive condition i am performing this condition below)
I am able to launch the previous activity but it is not canceled, its
just reloaded
Intent intent = new Intent(getActivity(), SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
getActivity().finish();
You need to make your Activity2 as "top task" with a Flag when you call it as follows:
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
See the Reference:
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished.
Then, call finish() in your Activity2. This should do the trick.
There is a way to kill your application process, but it is not recommended
this.finish();
Process.killProcess(Process.myPid());
This will kill your application and releases all the memory associated with it.
Note: Assuming your app has a single process id
Do like this
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);
Activity1.this.finish();
Intent intObj=new Intent(this, Homescree.class);
intObj.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intObj);

Clearing all activities in android

I have 4 Activities 1.Home,2.B,3.C and 4.D. Whenever I start Home from Activity D I want to finish all other activities. I Used this code, but when I press back button from Home it brings me to the previous activity. What I did wrong here.?
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent)
You can try this,
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Note: As described in FLAG_ACTIVITY_CLEAR_TOP documentation
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This will work only for activities which are still in the activity stack. I believe you are finishing the Home Activity when going to B. So that CLEARTOP won't work.
Now try something like this.
You need to set an Extra with intent Of "D" to Home. Then you have to check the Intent extra in Home, call finish() if the extra matching
Intent intent = new Intent(contxt, Home.class);
intent.putExtra("urString",defaultvalue);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
// Checking the intent extra at "HOME"
if(getIntent().hasExtra("urString")){
// manage your own way
finish();
}
In manifest.xml file set android:nohistroy="true" for all the activities

Re-launch current activity using intent

For an App I am developing, I want to re-launch the current activity using an intent. So I'm in MainActivity.class and I want to re-launch MainActivity.class using the following:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
This calls onDestroy() but does not re-launch the activity. Why doesn't this work?
If your are in an Activity: this.recreate();
If your are in a Fragment: getActivity.recreate();
Related Links :
How do I restart an Android Activity
how do I restart an activity in android?
Android activity restart
You could just use:
finish();
startActivity(getIntent());
Which will finish the current activity, and start a new activity with the same intent that you received when the activity was originally created. This should effectively re-launch the activity as is.
Edit:
See Reload activity in Android
Just do this:
Intent i=getIntent();//This simply returns the intent in which the current Activity is started
finish();//This would simply stop the current Activity.
startActivity(i);//This would start a new Activity.
Include this one ...
startActivity(intent);

how I start activity which is caller and close activty which is callling at same time?

I want to call a activty but when I call actviy I want to finish my caller activty , How can I do this?
A:caller
B:calling
startActivity(new Intent(A.this, B.class));
finish();
I write this code but everything is closing.
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This launch mode can also be used to good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task,
it will bring any currently running instance of that task to the
foreground, and then clear it to its root state. This is especially
useful, for example, when launching an activity from the notification
manager.
Try to do this:
startActivity(new Intent(A.this, B.class));
A.this.finish();

Categories

Resources