0

I'm stuck in one problem from yesterday and I can't find a solution. I'm trying to set one variable in a child activity in my tabhost to 0. Basically I have this situation :

Activity A -- sends extra "something" to activity B (B is getting it)
Activity B -- starts Activity C
Activity C -- onButton click sends extra "something" to activity B and finish()

This all happens in First tab. The problem is when I change the tab and return back to Activity A, than B, that extra "something" is still saving the value which Activity C sends, no matter if I send it from Activity A. Here is an example code :

Activity A -- starts B :

if (ownedCards != 0) {
        ownedStampii.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                  Intent previewMessage = new Intent(getParent(), OwnedStampii.class);
                  TabGroupActivity parentActivity = (TabGroupActivity)getParent();
                  previewMessage.putExtra("collection_id", collId);
                  previewMessage.putExtra("SOMETHING", 0);    // this is what I'm trying to se to 0
                  parentActivity.startChildActivity("OwnedStampii", previewMessage);

            }
        });  
}

This is how I read extra in Activity B :

final int extra = getParent().getIntent().getIntExtra("SOMETHING", 0);
Log.e("", "extra : " + extra);

This is how I send extra from Activity C :

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    Intent intent = getParent().getIntent();
    intent.putExtra("SOMETHING", objectID); // objectId of category which will sort the data
    getParent().setResult(RESULT_OK, intent);
    finish();
}
 });

I tried to set int extra =0 on onResume(), onRestart(), onStop().. and etc. it's still not working.

Any suggestions how can I set this variable to 0?

Android-Droid
  • 14,365
  • 41
  • 114
  • 185
  • We need to see more code to help you. Specifically activity B's `onCreate()`, then the function which opens activity C, and `onActivityResult()`. Then... Do you read "extra" in `onCreate()` or in `onActivityResult()`? Your question is also pretty confusing... is this is the order to reproduce the issue when walking through activities: A -> B -> C -> B -> A -> B ?? Do you always change activity via button clicks or do you press back at some point too? At which point? – Jarno Argillander Nov 21 '11 at 11:12

3 Answers3

2

I think you are reusing an existing intent in activity C and putting another SOMETHING into the same intent. Try calling intent.removeExtra("SOMETHING"); before putting it in again. Or send a new Intent from C to B.

Intent intent = getParent().getIntent();
intent.removeExtra("SOMETHING"); // Hox! Add this.
intent.putExtra("SOMETHING", objectID);
getParent().setResult(RESULT_OK, intent);
finish();
Jarno Argillander
  • 5,885
  • 2
  • 31
  • 33
1

You should pass data between Activities through Intent and startActivityResult() and onActivityResult();

This may be tricky while using TabGroupActivity.

Here is a solution which I posted to another question having somehow same scanario.

Community
  • 1
  • 1
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
1

You are declaring extra as a final variable which means once you create and assign it to something you cant change it afterwards.

So when you are trying to reassign and change the value it's basically not possible cause your variable is final.

Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32