2

My main activity is

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyGame(this));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

where whole content is with in Mygame class,and I want whole app to restart on click of button which is in MyGame class, how can I do so, I came across this and tried it but nothing worked

OnClickListener retryClicklistener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

//          Intent i = getContext().getPackageManager()
//                   .getLaunchIntentForPackage(getContext().getPackageName() );
//                   
//                   i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
//              //   startActivity( MyGameLongClick);
//          Intent startActivity = new Intent();
//          startActivity.setClass(this,OTHER_ACTIVITY.class);
//          startActivity(startActivity); 


        }};
Community
  • 1
  • 1
user2609847
  • 185
  • 2
  • 12

3 Answers3

3

may be recreate() can help you out..

DjP
  • 4,537
  • 2
  • 25
  • 34
2

If that's the main activity, you can use the following method to restart the application:

private void restartSelf() {
    AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + 1000, // one second
            PendingIntent.getActivity(this, 0, getIntent(), PendingIntent.FLAG_ONE_SHOT
                    | PendingIntent.FLAG_CANCEL_CURRENT));
    finish();
}

And call restartSelf() from onClick():

@Override
public void onClick(View v) {
    restartSelf();
}

This will restart your app after one second of button press. Hope this helps.

ozbek
  • 20,955
  • 5
  • 61
  • 84
0

I don't think You can do like this. You cant restart activity not whole application.

because Life-cycle of android app is stack. and activity on activity you can not close all the activity at a time. Read this and this.

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80