Activity with Intent.FLAG_ACTIVITY_NEW_TASK has been destroyed - android

I'm using this way to start activity from Service, if my app is background:
Intent intent = new Intent(this, FromBackgroundActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
it works perfect,
but if FromBackgroundActivity is active and I press Home key it puts all to background.
And if I click on my app in taskbar it restores my app to foreground but without FromBackgroundActivity, it is destroyed. (
how can i restore my app from background with FromBackgroundActivity?

Try intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
It'll clear all the task and go to the FromBackgroundActivity.

Related

How to resume app from background like tapping app launcher icon?

I'm developing a sticker keyboard for Android. In the keyboard layout, I have a button. When I minimize the app then open the keyboard and I tap the button, I want to open my app on the latest screen.
There are steps:
Open app (LauncherActivity)
Go to Activity 1 (TheFirstActivity)
Go to Activity 2 (TheSecondActivity)
Minimize the app
Open another app and open my keyboard
Tap the button.
When I tap the button, I want my app to be opened at the Activity 2 and the app state is restored like the state when the app is minimized.
This is my code:
Intent intent = new Intent(this, LauncherActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Use this
Intent intent = new Intent(this, yourActivity.class);
// set the new task and clear flags
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
use this code for this
Intent intent = new Intent(this, LauncherActivity.class);
// set the new task, clear flags and From History flag
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
startActivity(intent);
I'll take this scenario :
I assume you have a background service from where you want to resume your activity in a paused state in the background
and also, you just want to resume the activity with all his previous state as the system does when your app is on pause or like it's done when the notification is clicked.
For that case, you can use the code below
public void resumeActivityFromBackground(Intent intent, boolean restart) {
if (intent == null)
intent = new Intent(BackgroundServiceClass.this, MainActivity.class);
intent.putExtra("activity_started_from_service", true);
intent.setFlags(
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
| Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP
/*| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED*/);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
//PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
startActivity(intent);
stopSelf(); //Use this to stop the service if you are launching from a background service.
}
Intent.FLAG_ACTIVITY_NEW_TASK - Will is what helps to bring the existing activity from background without recreating a new instance.
Intent.FLAG_ACTIVITY_CLEAR_TOP - Will make sure that the activity your are resuming is actually the one on top
This is what worked for me.

Finish is not working - activity

I want to finish my activity, tried:
finish();
Activity.this.finish();
finishAffinity();
killing process...
It's how I am opening activity from service:
Intent a = new Intent(getApplicationContext(), Axctivity.class);
a.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
stopSelf();
After finish, activity is succesfully hidden but still stay on runnining apps list when I can restore it.
It's how finish() works. It doesn't destroy the object nor the activity.
The android OS keeps the processes around to speed up getting back into the applications.
See what exactly Activity.finish() method is doing?

Kill Activity after I log out Android Studio

I have this app which works with a current user saved in shared preferences, the app works fine but when I want to log out, I delete all the shared preferences and then it goes to the login activity, but if press the back button it goes back to my app's home activity. So, my question is there is a way to kill al the activities and history in Android Studio.
Try to start your Login Activity with flags:
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
It would start A as a root activity of a task and finish other activities.

Other activity is also brought to front on StartActivity

I have two activities (A and B) in my app and some BroadcastReceiver.
I encounter the following scenario:
A is running and was closed using the home button (onStop was called).
Some time after, BroadcastReceiver was triggered with some intent. It run the following code:
Intent activityIntent = new Intent(context,
B.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(activityIntent);
And B is indeed started, however A is also brought to front (behind B). How could I avoid A being fronted?
when you are pressing home button you are not closing the app actually(i mean it's in pause state) and it's in back stack and whenever you started a new activity of same app and close that activity it pops out the top activity in back stack.. So if you don't need this thing happen then please try following code
#Override
public void onStop() {
if(!isFinishing())
finish();
super.onStop();
}
From BoardcastReceiver you can start Activity B using this intent filter. This will clear the activity stack and pop Activity A from the stack.
Intent intent = new Intent(context,B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);

reopen app from background

When user press "Home Button" my app go to Background, but is still running. I need bring my app to front again. For example i've this code:
Context ctx=getApplicationContext();
Intent i =ctx.getPackageManager().getLaunchIntentForPackage("com.example.test");
ctx.startActivity(i);
but this code is to open app in different activity, there are a method or something to do this "correctly"?
You can simulate the "launching" of the app the same way that Android launches the app when the user selects it from the list of available apps. If the user starts an application that is already running, Android just brings the existing task to the foreground (which is what you want). Do it like this:
Intent intent = new Intent(context, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting
// the activity from a service
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
(Source: https://stackoverflow.com/a/12075313/3529926)
You can set this flag to your intent
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
and set the launch mode of your activity to single task in manifest (add this is your activity tags):
android:launchMode="singleTask"

Categories

Resources