0

I have some activities basically set up as shown below (clicks on are ListViews).

Parent class method to go to MyChild1

public void onItemClick(int pos){
    Intent i = new Intent(this, MyChild1.class);
    i.putExtra("KEY1", myAdapter.getItem(pos).getId());
    startActivity(i);
}

MyChild1 class method to go to MyChild2

public void onItemClick(int pos){
    Intent i = new Intent(this, MyChild2.class);
    i.putExtra("KEY2", myAdapter2.getItem(pos).getId());
    startActivity(i);
}

So you see that I have a parent, a child, and a grandchild activity. The child inflates based on the id provided by the parent and the grandchild inflates based on the id of the child. This works fine. However, when I use up navigation from the grandchild back to the child, it no longer has the id it needs from the parent to properly inflate. I need to support up navigation because the child can change based on actions in the grandchild.

I could pass the parent's id all the way down to the grandchild activity, but I don't know how to pass it back up. How can I handle this?

Edit: More code for context.
Here is my activity's onCreate.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new CourseActivityFragment()).commit();
            Intent intent = getIntent();
            courseId = intent.getIntExtra(MainFragment.COURSE_ID, 0);
            System.out.println("the saved state was null");
                    // The above prints, so I know it enters this if statement
        } else {
            courseId = savedInstanceState.getInt(COURSE_ID);
        }
    }

And here is my onSaveInstanceState

@Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        // Save current CourseId
        savedInstanceState.putInt(COURSE_ID, courseId);
        // Call superclass to save view hierarchy state
        super.onSaveInstanceState(savedInstanceState);
    }
NSouth
  • 5,067
  • 7
  • 48
  • 83

2 Answers2

2

Instead of startActivity, use startActivityForResult:

Intent intent = new Intent(ParentClass.this, ChildClass.class);
startActivityForResult(intent, RequestCode);

In child class, set some parameter in intent to identify in Parent class.

Intent intent = getIntent();    
intent.putExtra(<Key>, <Value>);

In parent class, override the onActivityResult method, and check your Value for that Key

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
     if (requestCode == RequestCode)
     {
          //Do something   
     }
}
Green goblin
  • 9,898
  • 13
  • 71
  • 100
1

I don't quite follow what you need so I hope this will help. Save it in your Activity class as member variable. Activity's object is not destroyed right after user leave this activity. If that still occur and Android will kill your activity, you can use onSaveInstanceState() and restoreInstanceState() methods to restore this id.

Michal
  • 1,008
  • 1
  • 12
  • 16
  • I read up on this and it seems like the way to go. But it's not working. I can see that my code is calling my overriden `onSaveInstanceState` method as I would expect, but when my `onCreate()` checks for if the savedInstanceState is `null`, it claims that it's null. Shouldn't it be !null if I've saved an instance state? – NSouth Apr 23 '14 at 01:04