Search code examples
javaandroid-studioandroid-activityandroid-lifecycle

Failing moving data from Activity C through Activity B to Activity A


I am trying to move some data and the thing is, when I jump directly from Activity A to Activity C (hereafter known as A and C), do some changes and pressing a button, I have no loss in data, when going back from C to A. It updates my different textboxes, seekbars, etc. perfectly.

But when I go from A to C via B and then back - through B again - to A, I don't get my data with me. A is not updated with the changes made in C.

Here is what I've tried (I've tried to only show the code, which I find relevant):

Activity A (Main):

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

    if(savedInstanceState!=null)
        games= (ArrayList<Game>) savedInstanceState.getSerializable("SAVED");
    else
        loadArrayFile();

...
A lot of unrelated code ..
...
// Here I want to go from A to B
        listMyGames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
            Game game = (Game) arg0.getItemAtPosition(position);
            Intent intent = new Intent(MainActivity.this, SpecialsActivity.class);

            intent.putExtra("game", game);
            intent.putExtra("pos", position);
            intent.putExtra("image", game.getImage());

            startActivity(intent); <<<<<<<< HERE IS THE ERROR
        }
    });

This part works fine, I guess.

I then have a onActivityResult-method inside Activity A - to handle the "input" from B, when I go from C to B to A:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            Game updatedGameSpecials = (Game)data.getSerializableExtra("updatedGameSpecials");
            int position = data.getIntExtra("pos", -1);

            adapter.remove(adapter.getItem(position));
            adapter.insert(updatedGameSpecials, position);
        }
    }
}

...
...

That's what I think is related in A. Moving on to Activity B:

// Inside the onCreate-method:
...
    btnGoToChangesActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    Intent intent = new Intent(SpecialsActivity.this, ChangesActivity.class);

        intent.putExtra("game", game);
        intent.putExtra("pos", position);
        intent.putExtra("image", game.getImage());
        startActivityForResult(intent, 1);
    }
});

But I also have a onActivityResult-method in B that goes like this:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            Game updatedGameSpecials = (Game)data.getSerializableExtra("updatedGameSpecials");
            int position = data.getIntExtra("pos", -1);
            data.putExtra("updatedGameSpecials", updatedGameSpecials);
            data.putExtra("pos", position);
            setResult(RESULT_OK, data);
            finish();
        }
    }
}

And finally my Activity C. Here i do some changes - rate a subject using a seekbar, fill out a textBox, etc.

I have a button, that when I press it, it should go to B (with the changes made in C), from there directly to A. The reason for this and not doing a straight jump from C to A and "finish()" B, when going from B to C is that I can't finish() B, because I have a "cancel"-button in C, that when pressed takes me from C to B:

btnSaveChanges.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    game.setGameChanges(etGameChanges.getText().toString());

    Intent data = new Intent();

    position = getIntent().getIntExtra("pos", -1);
    data.putExtra("pos", position);
    data.putExtra("updatedGameSpecials", game);
    setResult(RESULT_OK, data);
    finish();
}
});

Please notice, that when using a onItemLongClick inside Activity A - then it somehow works... But that's not the intention. The intention is to go to C via B and from C to A via B... So when I use this function and go from A to C and back again, then the data (/changes in C) isn't lost, but updated in A:

listMyList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    Game game = (Game)parent.getItemAtPosition(position);
    Intent intent = new Intent(MainActivity.this, ChangesActivity.class);

    intent.putExtra("game", game);
    intent.putExtra("position", position);
    intent.putExtra("image", game.getImage());
    startActivityForResult(intent, 1);
    return true;
 }
});

What am I missing or doing wrong? I can't see any errors and in my opinion I putExtra the right places, but I am clearly missing something..


Solution

  • After trying different things for hours, I found out, that it all came back to using

    startActivity(intent)
    

    instead

    startActivityForResult(intent, 1)
    

    inside Activity A...