如何检查上次运行活动?

时间:2014-05-10 09:27:10

标签: android android-intent android-activity

假设我有5个活动,A,B,C,D,E。 C是中间活动,它决定调用C的活动是否为A,然后C应该将用户带到D,如果调用C的活动是B,那么C应该将用户带到E.那么有没有办法确定上次运行哪些活动(A或B),以便我可以在C(D或E)之后将用户带到相应的活动中?

这有点复杂,但这是我解释这个问题的最好方法。

4 个答案:

答案 0 :(得分:1)

在A和B使用此

A:

Intent intnt = new Intent(this, C.class);
intnt.putExtra("source","A");
startActivity(intnt);

B:

Intent intnt = new Intent(this, C.class);
intnt.putExtra("source","B");
startActivity(intnt);

在C,onCreate()

String src = getIntent().getStringExtra("source");
if(src.equlas("A")){
//start D
}else if(src.equlas("B")){
//start E
}

答案 1 :(得分:1)

// try this way,hope this will help you...

        A Activity code
        Intent intent = new Intent(A.this,C.class);
        intent.putExtra("fromActivity","A");
        startActivity(intent);

        B Activity code
        Intent intent = new Intent(B.this,C.class);
        intent.putExtra("fromActivity","B");
        startActivity(intent);

        C Activity code
        if(getIntent().getStringExtra("fromActivity").equals("A")){
            Intent intent = new Intent(C.this,D.class);
            startActivity(intent);
        }else{
            Intent intent = new Intent(C.this,E.class);
            startActivity(intent);
        }

答案 2 :(得分:1)

您可以使用Intent附加功能。

在A& B:

Intent intent = new Intent(this, C.class); // Or however you do it now 
intent.putExtra("caller", getClass()); // getClass must be called on the activity class here
// Do any stuff you want here to the Intent 
startActivity(intent); // Or however you do it now 

onCreate的C:

Intent intent = getIntent();
Class caller = (Class) intent.getSerializableExtra("caller");
if(caller == A.class) // A called C

等等。甚至更好,使用目标类。

在A& B:

Intent intent = new Intent(this, C.class); // Or however you do it now 
intent.putExtra("target", D.class); // What should be called from C is put here 
// Do any stuff to the Intent you want here 
startActivity(intent); // Or however you do it now 

onCreate的C:

Intent intent = getIntent();
Class target = (Class) intent.getSerializableExtra("target"); // Put this class somewhere 

现在要为D / E创建意图:

Intent intent = new Intent(this, target);

答案 3 :(得分:0)

您可以随时在调用活动时使用bundle发送信息,并在被调用的活动中读取该信息。在您的情况下,您可以这样做:

来自活动A:

Intent i = new Intent(A.this,C.class);
i.putExtra("LAUNCHER", "A");
startActivity(i);

来自活动B:

Intent i = new Intent(A.this,C.class);
i.putExtra("LAUNCHER", "B");
startActivity(i);

和C的内部oncreate(),读取方式如下:

Bundle extras = getIntent().getExtras();
String code = extras.getString("LAUNCHER");

if(code == "A"){

Intent i = new Intent(C.this,D.class);
startActivity(i);
}else{
Intent i = new Intent(C.this,E.class);
startActivity(i);
}