Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiating between an Activity launch from home screen or from another activity from App

I need to know a generic way to distinguish between a call of activity from launcher and a call from another activity from inside my app, or a BACK on the activity stack

Anyone? this is bugging me for quite a while now and i need to put it to rest...

Thanks in advance JQCorreia

like image 282
JQCorreia Avatar asked Apr 12 '11 15:04

JQCorreia


People also ask

Where would we specify which activity should launch first in app?

The intent-filter inside the activity tells Android which Activity to launch.

How do I run an activity from another application?

If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows: Intent LaunchIntent = getActivity(). getPackageManager(). getLaunchIntentForPackage(CALC_PACKAGE_NAME); startActivity(LaunchIntent);


2 Answers

In the onCreate of your Activity, call getIntent(). If the Activity is started from the launcher (home screen) the values for getAction() will be android.intent.action.MAIN and the getCategories() will return a set which will contain the android.intent.category.LAUNCHER category. If the activity is started from elsewhere these values may be null.

like image 77
advantej Avatar answered Sep 21 '22 07:09

advantej


In addition to @advantej's answer, you can extend each start-call to that activity adding an extra to the starting intent (e.g. intent.putExtra("caller", this.getClass().getSimpleName());

In the activity's onCreate method you can check then what @advantej suggests.

If the initiator is not the home-screen icon, than you can check further if the intent.hasExtra("caller") returns true, and if so, what is it.

like image 39
rekaszeru Avatar answered Sep 21 '22 07:09

rekaszeru