Reputation: 161
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 !
Upvotes: 1
Views: 2259
Reputation: 578
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:
Upvotes: 2
Reputation: 3406
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.
Upvotes: 0