Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting intent extra and the onCreate method?

I have classes A, B and C. Class A sends intent to B, B runs C, C returns to B.....but then inside of the onCreate of the B class it wants the intent of Class A. But because its come from class C it does not get it but I still need the intent of class A

Any idea on how to get around this? I guess one solution might be to store the extra.getString in a database or similar?

Bundle extras = getIntent().getExtras();
 newString = extras.getString("ID"); 
like image 710
user1876202 Avatar asked Mar 10 '13 20:03

user1876202


People also ask

How do I get extras from an intent?

We can use the Intent. getExtras() in the target activity/class to get the attached bundle and extract the data stored in it. String user_name = extras.

What is the onCreate method?

Android App Development for BeginnersonCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.

What is the name of the method which is use to get intent data in onCreate ()?

There is no difficulty using intents to start NextActivity from within MainActivity if the getIntent() method is called inside the onCreate() block of NextActivity : public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.

What is the purpose of onCreate?

onCreate(savedInstanceState); calls the method in the superclass and saved InstanceState of the activity if any thing damage the activity so its saved in instanceState so when reload the activity it will be the same before.


2 Answers

Since I do not know exactly your Activities flow, this is a solution but may not be the appropriate one.

When you start new activity, put an extra

intent.putExtra("ID_FROM_A", value); // except for ActivityA value = mIdFromA
startActivity(intent);

On the receiving activity

onCreate()
{
    mIdFromA = getIntent().getStringExtra("ID_FROM_A");
}
like image 96
Hoan Nguyen Avatar answered Oct 02 '22 06:10

Hoan Nguyen


So if I've got it right:

Activty A:

  • Creates intentAtoB
  • Starts Activity B

Activty B

  • Receives intentAtoB
  • Creates intentBtoC
  • Starts Activity C

Activity C

  • Receive intentBtoC
  • does something

Now: Does Activity C:

  1. return to Activity B using finish(), or

  2. start Activity B again?

If 1, when you return to Activity B, it will still have everything you set up when it was first created. So you need to extract everything from the intent in the onCreate of B.

If 2, you will simply need to pass the information down the chain in the intents you use for starting each activity.

If you could (a) confirm what the sequence if and (b) clarify why the above wont work, I'm sure we can move forwards.

like image 41
Neil Townsend Avatar answered Oct 02 '22 04:10

Neil Townsend