문제

Suppose I have 5 activities , A, B, C, D, E. C is the middle activity which decides if the activity that invoked C is A , then C should take the user to D , and if the activity that invoked C is B , then C should take the user to E. So is there any way to determine which of the activities were last run(A or B) so I can take the user to the corresponding activity after C(D or E)?

It is a bit complicated ,but this is the best way I can explain this.

도움이 되었습니까?

해결책

at A and B use this

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);

at C, onCreate()

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

다른 팁

// 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);
        }

You can use Intent extras.

In 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 

In onCreate of C:

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

And so on. Or even better, use the target classes.

In 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 

In onCreate of C:

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

And now to create the Intent for D/E:

Intent intent = new Intent(this, target);

you can always send information with intent using bundle while calling activity and read that information in called activity.In your case, you can do it like this:

from activity A:

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

from activity B:

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

and Inside oncreate() of C, read it like this:

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);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top