Android: Get intent and reroute it to another class
My question is simple: if an intent object exists, I want to merely use that and add putExtra() and then startActivity to go back. However, I have no idea how to do this. Any help is appreciated.
Intent intent;
if(getIntent() != null) {
intent = getIntent();
//reroute to BarActivity class
} else {//this part is fine
in开发者_运维技巧tent = new Intent(FooActivity.this, BarActivity.class);
}
intent.putExtra("cardHP", "100");
startActivity(intent);
getIntent() is a part of a larger chain. It will return the ENTIRE intent that was sent.
You can retrieve the calling class by using getIntent().getExtras().getString("TheOriginatingClass");
Edit: ok last time I edit this... I am too tired, need to go to sleep after this lol... In the intent that was sent, there is the originating class and the class that is being called (Class1 and Class2). When you use the intent that was sent by Class1 you are essentially trying to call Class1 with the same intent that called Class2. You need to create a new intent and if you need the originating class (Class1) you can get that via the chain I described above.
Note: I forgot to add you will need to put the classname in as a string extra in the intent that is called from Class1. AFAIK there isn't a direct way to find out what the calling class was. I might be wrong about that though.
-or-
If you are using an int (as you are in your example) you could use startActivityForResult(Intent, int) http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)
If we use the startActivityForResult method, and detect the returned result on the onActivityResult method, then this works.
精彩评论