android check if an activity was started from an action or from another activity?
In my manifest file, I have an activity declaration which looks something like this:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
开发者_StackOverflow社区
This activity is obviously launched on application start. User can navigate from this activity to another activity and from that one to another etc. In another activity, I start MainActivity by using 'startActivity' method, ie. something like:
Intent intent = new Intent(AnotherActivity.this, MainActivity.class);
startActivity(intent);
In my MainActivity
(in onCreate() method maybe), can I determine whether an activity was started from a action of from another activity? Is there something like "launcher listener"? I would like to avoid putting any extra content in the intent.
Can I simply put String s = getIntent().getAction();
in onCreate method and check whether it is has a value of MAIN?
Well the stock android launcher does send Intent.ACTION_MAIN
as as the action. However, you can't be sure that some other launcher will have the same behaviour. Your best bet will be to pass some extra data with the Intent.
精彩评论