erfan
erfan

Reputation: 13

how can go to previous activity with current state

have two Activity , in my Activity1 have button go to Activity2

 Intent mIntent=new Intent(MainActivity.this, MainA2ctivity.class);
 startActivity(mIntent);

in my Activity2 have player with play resume button and seek bar which when play song that become like this :

enter image description here

and this is my press back code in Activity2:

@Override
public void onBackPressed()
{

    Intent mIntent=new Intent(Main2Activity.this, MainActivity.class);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(mIntent);

}  

in Activity1 have another button which i want when click that go to activity 2 with current play pause button and seekbar and this is my code :

 Intent mIntent=new Intent(MainActivity.this, Main2Activity.class);
 startActivity(mIntent);

but problem is when go in acitvity2 song is playing but seekbar and play pause button still like first time : enter image description here

Upvotes: 1

Views: 301

Answers (2)

Tiziano Munegato
Tiziano Munegato

Reputation: 929

You have to send state data from MainActivity to Main2Activity back and forth

MainActivity code

final int REQUEST_PLAYPROG = 22; // custom int code

Intent mIntent = new Intent(MainActivity.this, Main2Activity.class);
mIntent.putExtra("playState", isPlaying);
mIntent.putExtra("progress", seekbarProgress);
startActivityForResult(mIntent, REQUEST_PLAYPROG);

In Main2Activity you will retrive data like this

Bundle bundle = getIntent().getExtras();
if(bundle!=null) {
    boolean isPlaying = bundle.getBoolean("playState");
    int seekbarProgress = bundle.getInt("progress");
}

And transfer from A to B is done, now you have to send back data from B to A onBackPressed

This is Main2Activity code

@Override
public void onBackPressed() {
    Intent mIntent = getIntent();
    mIntent.putExtra("playState", isPlaying);
    mIntent.putExtra("progress", seekbarProgress);
    setResult(RESULT_OK, mIntent);
    finish();
}

And finally retrive data in MainActivity through onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==REQUEST_PLAYPROG && resultCode==RESULT_OK) {
        boolean isPlaying = data.getExtras().getBoolean("playState"), 
        int seekbarProgress = data.getExtras().getInt("progress"), 
    }
}

Upvotes: 1

Karun Shrestha
Karun Shrestha

Reputation: 733

You can use onSaveInstanceState to save the activity state, and onRestoreInstanceState to restore the state, Here's an example:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate if the process is
    // killed and restarted.
    savedInstanceState.putInt("play", true);
    savedInstanceState.putString("playtime", "1:20");
    // etc.
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Restore UI state from the savedInstanceState.
    // This bundle has also been passed to onCreate.
    play = savedInstanceState.getBoolean("play");
    playtime = savedInstanceState.getString("playtime");
}

Remember this won't work if the activity is killed.

Upvotes: 0

Related Questions