Skip to content Skip to sidebar Skip to footer

How Create Homescreen Shortcut To Resume Top Activity

I have a litlte code to add a shortcut to homescreen for the first running time: Intent shortcutIntent = new Intent(getApplicationContext(), SFlashActivity.class);

Solution 1:

Use the isTaskRoot() method Enter the following code snippet to your OnCreate() of your main activity Here is an example:


@Override public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.splashscreen);              
        if(!isTaskRoot()){
            finish();
            return; 
        }
 }

Found the solution here:

Solution 2:

When launched via icon on the home screen, Android will always start the activity with the android.intent.action.MAIN filter in your AndroidManifest.xml, unless the application is already running (in which case it will obviously restore the activity on top of the stack).

To achieve what you described you can simply store the last visible activity in SharedPreferences and have a Dispatcher activity that starts the last activity according to the preferences.

So if activity is present in prefs start that activity or start SplashScreen.

In every activity you want to re-start automatically:

@OverrideprotectedvoidonPause() {
    super.onPause();

    SharedPreferencesprefs= getSharedPreferences("X", MODE_PRIVATE);
    Editoreditor= prefs.edit();
    editor.putString("lastActivity", getClass().getName());
    editor.commit();
}

And a Dispatcher activity similar to the following:

publicclassDispatcherextendsActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Class<?> activityClass;

        try {
            SharedPreferencesprefs= getSharedPreferences("X", MODE_PRIVATE);
            activityClass = Class.forName(
                prefs.getString("lastActivity", SplashScreen.class.getName()));
        } catch(ClassNotFoundException ex) {
            activityClass = SplashScreen.class;
        }


        startActivity(newIntent(this, activityClass));
    }
}

Remarks

  • You could create a base class for the onPause override
  • The Dispatcher activity obviously needs to be the android.intent.action.MAIN action

Ref : - How to make an android app return to the last open activity when relaunched?

Solution 3:

I also went through the same problem of resuming the application while coming back when the app is in background because of the Home button press,

Two changes To be done

1.Add below property to the activities in manifest file

android:alwaysRetainTaskState="true"

This will resume the activities when coming from the launcher icon.

2.Upper change wont resume the application if you click on the app icon on Home screen which you have created programatically. Because you have specified the "SFlashActivity.class" for the launching purpose. To overcome this you will have to do a trick as below:

Add this function to your SFlashActivity.class

publicbooleanCheckIfAppIsResumable(){
    try{
         ActivityManageram= (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
         List<RunningTaskInfo> runningTaskInfoList =  am.getRunningTasks(1);
         Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
         while(itr.hasNext()){
             RunningTaskInforunningTaskInfo= (RunningTaskInfo)itr.next();
             CharSequence desc= runningTaskInfo.description;
             intnumOfActivities= runningTaskInfo.numActivities;
             int numRunning=runningTaskInfo.numRunning;
             StringtopActivity= runningTaskInfo.topActivity.getClassName();
             Log.d("description", ""+desc);
             Log.d("numActivities", ""+numOfActivities);
             Log.d("numRunning", ""+numRunning);
             Log.d("topActivity", ""+topActivity);
             if(numRunning>1 && topActivity.equalsIgnoreCase("com.yourpackage.yoursplashclass"))
                 returntrue;
         }
         returnfalse;
    }
    catch(Exception e){
        Log.d("Errror CheckIsAppIsResumable=", ""+e.getMessage());
        returnfalse;
    }
}

And in Oncreate:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    if(CheckIfAppIsResumable()){
        finish();
        return;//will finish the new started splash activity
    }

Provided that your shortcut intent does not have flag FLAG_ACTIVITY_CLEAR_TOP.

Post a Comment for "How Create Homescreen Shortcut To Resume Top Activity"