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

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.

Related

How to clear existing task and start entirely new task in Android?

In my Android app, I am launching my app from notification using below activity flags:
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_CLEAR_TOP
Still some activities of old task are getting visible on new launch.
Am I missing any flag?
Try this,
Intent intent = new Intent(MainActivity.this, Second.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
Intent loginscreen = new Intent(this, HomeActivity.class);
loginscreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(loginscreen);
this.finish();
I agree with #fab It is as good as logout functionality where in you want t clear all the flags values set by the user .
To Achieve the target android has provided Activity launch modes to launch activity
For detailed description go through Link
You can add this line before you create the PendingIntent, but after that it depends of what you want to do exactly:
targetIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Android Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK do not work from Dialog

I don't want user to be able to go somewhere back from my LoginActivity.
This works nice from MainActivity (from Navigation Drawer):
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Result - back stack is cleared.
But when I'm trying to call this from my custom Dialog:
private Context mContext;
Intent intent = new Intent(mContext, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
dismiss();
mContext.startActivity(intent);
((Activity)mContext).finish();
back stack isn't clear, I can go back from my LoginActivity to previous window.
Tried to search the reason, but no result till now.
Try clearing your activity stack:
Set android:noHistory= "true" in your AndroidManifest.xml file in the <activity tag for the login activity.
http://developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_noHistory
I had a similar problem and adding android:launchMode="singleTop" to my activity in AndroidManifest.xml did the work.
I took the idea from this question

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"

Activity with Intent.FLAG_ACTIVITY_NEW_TASK has been destroyed

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.

Open activity from notification Jelly Bean issue

Just to make it short:
what I need is open an activity, start a countdown, minimize, launch a notification when timer goes to 0 and by tapping the notification go back to the previous state of the activity without creating a new one
what I have is this:
Intent intent = new Intent(ctx, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
It works perfectly for android <4.2 but when runs on Jelly Bean, it opens a new instance of the Activity.
It seems that Jelly Bean does not recognize none of the flags Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
How can I make it run for 4.2+ ?
Just set your activity launch mode as
launchMode="singleTop"

Categories

Resources