background play study: use moveTaskToBack function

activity property set:
android:launchMode="singleInstance"  // is first important
android:clearTaskOnLaunch="true"

test example implement:

public class dddd extends Activity {
    private String TAG="ddddd";
    private final static  int sta = 10;
    private final int sta_l = sta+90;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG,"onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        showMessage("sta = "+sta_l);
    }
   
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.d(TAG,"onStart");
    }

   
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.d(TAG,"onResume");
    }

    public void showMessage(String text){
        Toast.makeText(dddd.this, text, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.d(TAG,"onPause");
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        //super.onBackPressed();
        Log.d(TAG,"onBackPressed");
        moveTaskToBack(true);
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.d(TAG,"onStop");
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }       
}
program flow:
when press BACK button, exec onBackPressed() func, ok, then onPause()->onStop()->return to main UI.
when reopen the app, exec onStart()->onResume().
so, use this way, we can implemnt background play.

你可能感兴趣的:(background play study: use moveTaskToBack function)