我可以从两个不同的活动,A和B.活动C有一键到达活动℃。我想检查,如果我去到C从A或C来自B.如果我从A到C,我想按钮做一件事,如果我选自B到C,我想按钮做另一件事。
是否有可能以检查其中previous活动是?
这样的事情...
否则,如果(ID == R.id.action_button){
如果(previosActivity == A){
意向意图=新的意图(这一点,NewActivity.class);
startActivity(意向);
}
否则,如果(previosActivity == b){
意向意图=新的意图(这一点,AnotherActivity.class);
startActivity(意向);
}
返回true;
}
解决方案
当你去活动ç
构成任何活动,您可以传递一个额外
通过意图。然后你就可以检索额外
在活动ç
并根据需要使用它
例如:(未经测试code)
当你去从A到C
意图mIntent =新的意图(这一点,ActivityC.class); //'这'是活动一
extras.putExtra(FROM_ACTIVITY,A);
startActivity(mIntent);
当你去从B到C
意图mIntent =新的意图(这一点,ActivityC.class); //'这'是b活动
extras.putExtra(FROM_ACTIVITY,B);
startActivity(mIntent);
现在在活动ç,您retreive这样的:
意图mIntent = getIntent();
字符串previousActivity = mIntent.getStringExtra(FROM_ACTIVITY);
然后你可以用
如果(previousActivity.equals(A)){...}
或
如果(previousActivity.equals(B)){...}
在你的问题。
I can reach activity C from two different activities, A and B. Activity C has one button. I would like to check if I went to C from A or to C from B. If I went from A to C, I would like the button to do one thing and if I went from B to C, I would like the button to do another thing.
Is it possible to check which the previous activity was?
Something like this...
else if (id == R.id.action_button) {
if (previosActivity == A) {
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
}
else if (previosActivity == B) {
Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);
}
return true;
}
解决方案
When you go to Activity C
form any of the activities, you can pass an extra
through the intent. Then you can then retrieve that extra
in Activity C
and use it as desired
Eg. (Untested code)
When You go from A to C:
Intent mIntent = new Intent(this, ActivityC.class); //'this' is Activity A
extras.putExtra("FROM_ACTIVITY", "A");
startActivity(mIntent);
When You go from B to C:
Intent mIntent = new Intent(this, ActivityC.class); //'this' is Activity B
extras.putExtra("FROM_ACTIVITY", "B");
startActivity(mIntent);
Now in Activity C, you retreive this:
Intent mIntent = getIntent();
String previousActivity= mIntent.getStringExtra("FROM_ACTIVITY");
Then you can use
if (previousActivity.equals("A")){...}
or
if (previousActivity.equals("B")){...}
as in your question.