How to Resume Android Activity Programmatically from Background

How to resume Android Activity programmatically from background

In order to bring your app to the foreground, you must call startActivity() from another context (either a Service or a BroadcastReceiver). Just calling startActivity() from within an Activity won't bring your app to the foreground.

You don't need the ACTION and CATEGORY in your Intent, but you do need to set Intent.FLAG_ACTIVITY_NEW_TASK.

How to programmatically resume an Activity from a notification?

After some trial and error testing, I got it working using the following combination of methods

PackageManager pm = context.getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(context.getPackageName());
intent.setPackage(null);
return PendingIntent.getActivity(context, 0, intent, 0);enter code here

The key part here was intent.setPackage(null);

/**
* (Usually optional) Set an explicit application package name that limits
* the components this Intent will resolve to. If left to the default
* value of null, all components in all applications will considered.
* If non-null, the Intent can only match the components in the given
* application package.
*
* @param packageName The name of the application package to handle the
* intent, or null to allow any application package.
*
* @return Returns the same Intent object, for chaining multiple calls
* into a single statement.
*
* @see #getPackage
* @see #resolveActivity
*/

So apparently, pm.getLaunchIntentForPackage(context.getPackageName()); returns an Intent limited to the components in context.getPackageName() and setting the package explicitly to null is needed afterwards so that all components are considered.

I'm not completely sure about ^ though. FWIW adding it, solved the issue ¯\_(ツ)_/¯

Passcode on resume from background

You can store your boolean value in shared preferences, that way it stays persistent in memory even if you switch Activities.
For example: when you want to unlock your app (for example in onStop()) you can just set it to false with this method:

public void setLockStatus(boolean lock) {
getSharedPreferences("SOMETAG", 0).edit().putBoolean("LOCK", lock)
.commit();
}

Later when you want to check if your app is locked (maybe in onStart of next activity):

public boolean getLockStatus() {
return getSharedPreferences("SOMETAG", 0).getBoolean("LOCK", true);
}

Also note that this method will return true if no "LOCK" value has been set (as indicated by the second param to getBoolean).

So every Activity that you have when it starts checks our flag if the App is locked.

@Override
public void onStart() {
super.onStart();
if (getLockStatus() == true) {
// show lockscreen
} else {
// we are not locked.
}
}

Now we need one more flag to check if we are still in the App and never left:

public void setAppStatus(boolean status) {
getSharedPreferences("SOMETAG", 0).edit().putBoolean("IN_APP", status)
.commit();
}
public boolean getAppStatus() {
return getSharedPreferences("SOMETAG", 0).getBoolean("IN_APP", false);
}

So now every time we launch a new activity before we start it we have to set a flag that we are still in the App, so that onStop will know that we shouldn't lock the app.
For example if your button onClick start a new Activity in the onClick we can do this:

    @Override
public void onClick(View v) {
setAppStatus(true); // we are not leaving the app.
// startActivity(blabla);
}

Now onStop checks if we need to lock:

    @Override
public void onStop() {
super.onStop();
if(getAppStatus() == false) setLockStatus(true); // locking the app
else setLockStatus(false);
}

EDIT: also you need to setAppStatus(false); if you are actually leaving the application.

Hope this gives you an idea of how to solve it, you need to implement the back press logic yourself (when to lock the app and when not to).

Resume another application from the background

just launch another app.

Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(another app packagename)

start this intent.
(you can also use intent by URI .And make sure suitable lauchmode)



Related Topics



Leave a reply



Submit