1

I have a litlte code to add a shortcut to homescreen for the first running time:

    Intent shortcutIntent = new Intent(getApplicationContext(),
            SFlashActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "New App");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    addIntent
            .putExtra("duplicate", false);
    getApplicationContext().sendBroadcast(addIntent);

But with above code, my app always start Splash screen althought my app is running. So how could i make home screen shortcut resume to top activity. I noticed that, app's shortcut made by google play on install always resume the top activity.

Thank so much !

redwind
  • 161
  • 1
  • 12
  • you should store data inside "SharedPreferences" when your application first time starts. – Chintan Rathod Jun 12 '13 at 06:08
  • The standard behaviour is to resume the top activity if your app is already running. You've got something strange going on if this isn't happening. I know you've already accepted an answer, but that answer is overkill and shouldn't be necessary. Add the contents of your manifest to the question. Perhaps there's something nasty in there. – David Wasser Jun 12 '13 at 08:03
  • Also note that there is a bug in Android that will show the behaviour that you described, if the app was initially started from the installer or an IDE (Eclipse, Android Studio, etc). To make sure you aren't seeing this bug: install your app on the device, don't open it by clicking "open" on the installer screen, now go to the home screen and start your app by clicking the app icon. See if your problem has now gone away. See http://stackoverflow.com/questions/11296203/on-click-of-shortcut-on-homescreen-launching-from-spalsh-screen-in-android – David Wasser Jun 12 '13 at 08:06

3 Answers3

2

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:

Gil
  • 568
  • 5
  • 9
1

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:

@Override
protected void onPause() {
    super.onPause();

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

And a Dispatcher activity similar to the following:

public class Dispatcher extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Class<?> activityClass;

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


        startActivity(new Intent(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?

Community
  • 1
  • 1
Pankaj Kumar
  • 81,071
  • 26
  • 167
  • 187
  • Thanks for your answer! i'll try and accpet this answer – redwind Jun 12 '13 at 06:47
  • i still wonder why shortcut made by google play always resume top activity ? – redwind Jun 12 '13 at 06:49
  • 1
    defiantly they did some logic from there side to store current state of Play, and when you starts the app they check last state. You can verify this.... close Play from any state. and goto settings of Play and clear the application data. And now open the Play.. you will see that you are on first screen of Play.. :D – Pankaj Kumar Jun 12 '13 at 06:53
  • This can't be right. The handling of the BACK stack would be completely broken if you implemented this. Also, I'm not sure that OP is talking about the shortcut for the "Play" app. I think he is talking about any shortcut for any app that is created automatically when an app is installed via "Play". – David Wasser Jun 12 '13 at 08:16
  • Are you dreaming here? If he talked about any shortcut they why did he provide his code to create shortcut? Am I wrong? – Pankaj Kumar Jun 12 '13 at 08:17
  • Hi David Wasser, hi Pankaj Kumar This's my mistake, sorry about my english, maybe my question's not clearly. Okie, my question like what David Wasser said :) and i post my code here because i don't know that my source code do right way like google play do for any app. Anyway, Thanks for all your help ! – redwind Jun 12 '13 at 16:08
0

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

public boolean CheckIfAppIsResumable(){
    try{
         ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
         List<RunningTaskInfo> runningTaskInfoList =  am.getRunningTasks(1);
         Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
         while(itr.hasNext()){
             RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next();
             CharSequence desc= runningTaskInfo.description;
             int numOfActivities = runningTaskInfo.numActivities;
             int numRunning=runningTaskInfo.numRunning;
             String topActivity = 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"))
                 return true;
         }
         return false;
    }
    catch(Exception e){
        Log.d("Errror CheckIsAppIsResumable=", ""+e.getMessage());
        return false;
    }
}

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.

Jaldip Katre
  • 2,716
  • 1
  • 18
  • 14