Question

If I press home button in my app. And start my app again from menu. Its start from first page. I want it to resume from previous activity. In other links on SO regarding this issue no answer is much satisfied.

Here's code-

public class Good1 extends Activity{

    private LinearLayout layout2; 
    MediaPlayer mg;
    boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.good);
        mg = MediaPlayer.create(this, R.raw.all);
        layout2 = (LinearLayout) findViewById (R.id.layout2);
        layout2.setOnClickListener(new OnClickListener(){
        public void onClick(View v) { 
        if(!flag){ 
         mg.start(); 
         layout2.setEnabled(false); 
         } 
        else
        { 
            flag =true ;
        }
      } 
    }); 

        mg.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
        public void onCompletion(MediaPlayer mg) { 
           flag=false; 
           layout2.setEnabled(true); 
          } 
       });

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override 
    public void onBackPressed() {
        super.onBackPressed();
        mg.stop();
        finish();
    } 

    @Override 
    public void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

}

No correct solution

OTHER TIPS

Configure with your activity using:

android:alwaysRetainTaskState="true"

and:

android:launchMode="singleTask" //(preffered, or "singleInstance")

For example:

//In the manifest file you have to add these.
<activity android:name=".MainActivity"
              android:label="@string/app_name"
              android:screenOrientation="portrait"
              android:alwaysRetainTaskState="true"
              android:launchMode="singleTask"> </activity>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top