Getting intent extra and the onCreate method?
Asked Answered
W

3

9

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"); 
Wamble answered 10/3, 2013 at 20:16 Comment(4)
Do you mean: A->B->C->A (as you write) or A->B->C->B (as you imply later)?Hyperkeratosis
Oh I apoligize, I meant A->B->C->BWamble
Is the only thing you want is the string from "ID"Schwarz
@Wamble Thanks - I've put the start of an answer below, if you could comment and answer the question in it I'll look again later today.Hyperkeratosis
H
2

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.

Hyperkeratosis answered 11/3, 2013 at 9:15 Comment(2)
Approach 1. return to Activity B using finish(), is better. Just store the values in Activity B at Class Level Variables, in onCreate() on Activity B. As I suggested you in my above answer.Zhang
@Zhang I agree that that is the better approach, I'm just trying to find out what the question author is actually doing so as to help them from where they are starting.Hyperkeratosis
S
8

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");
}
Schwarz answered 10/3, 2013 at 20:48 Comment(0)
H
2

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.

Hyperkeratosis answered 11/3, 2013 at 9:15 Comment(2)
Approach 1. return to Activity B using finish(), is better. Just store the values in Activity B at Class Level Variables, in onCreate() on Activity B. As I suggested you in my above answer.Zhang
@Zhang I agree that that is the better approach, I'm just trying to find out what the question author is actually doing so as to help them from where they are starting.Hyperkeratosis
L
0

You can get the intent from class A with a extra that prove "it's class A" and then set it to an local intent;

extras = getIntent().getExtras();
String a = extras.get("KEY");

if (a.equlas("A_CLASS") {
    class_a_extras = getIntent();
}
Luteous answered 4/2, 2020 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.