Android: Killing an activity

I have an activity where a user put his nickname. I want to not be able to go back after this. I have tried everything but no luck. Tried so far :

Manifest

 <activity
     android:name=".NewUserActivity"
     android:noHistory="true" />
 <activity android:name=".PvP.PVPWinningActivity" />

In Activity:

btnEnterDungeons.setOnClickListener(new View.OnClickListener() {          
     @Override
     public void onClick(View view) {           
        if(!TextUtils.isEmpty(editUsername.getText().toString().trim())) {
             uploadUserData();
             Intent intent = new Intent();
             intent.setClass(NewUserActivity.this, GameChooseActivity.class);
             intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
             startActivity(intent);
             finish();
 }

or in Activity again:

btnEnterDungeons.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
         if(!TextUtils.isEmpty(editUsername.getText().toString().trim())) {
             uploadUserData();
             Intent intent = new Intent();
             intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
             intent.setClass(NewUserActivity.this, GameChooseActivity.class);                    
             startActivity(intent);
             finish();
 }

None of these works. Actually, I want to completely kill the activity, no back button or any way to have access again.

Upvotes: 0

Views: 527

Answers (4)

simon
simon

Reputation: 133

In NewUserActivity, override OnBackPressed() method then call finish; to kill the current activity; See sample implementation below:

@Override public void onBackPressed() {

                    killActivity();

            })
            .setNegativeButton("No", null)
            .show();
}

private void killActivity() {
    finish();
}

Upvotes: 1

With Android's Newer Version >= API 16 use finishAffinity()

finishAffinity(); did the trick.

As i read everywhere suggested to use just finish(); For newer API (I use >= 17 ) had to use finishAffinity();

Upvotes: 0

Rohit Singh
Rohit Singh

Reputation: 18202

One-Time Setup?

Based on my understanding you want a one-time setup Activity. Your code is fine the only problem I see is that when you open the application again it starts from the beginning. It is because there is no logic in your code to check if you have already done a one-time setup. For this, you can use SharedPreference.
You can create a Splash Activity and check if the one-time setup is done or not based on a boolean flag.

So what I am suggesting is this first time the user opens the app
SplashActivity-->NewUserActivity--->GameChooseActivity
otherwise
SplashActivity-->GameChooseActivity

SplashActivity extends AppcompatActivity{

   boolean hasDoneSetUp = false;
   private SharedPreferences pref;
   private Editor editor;

   protected void onCreate(Bundle sis)
   {
      super.onCreate(sis);
      setContentView(R.layout.splash);

      pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
      editor = pref.edit();

      hasDoneSetUp = checkForSetUp();

      if(!hasDoneSetUp)
         startOneTimeSetUp();
      else
         startGameChooseActivity();

    }
}

How to do the onetime setup?

Set hasDoneSetUp true when the one-time setup is done and save this value in the SharedPreference so that when you open the application again it will check whether the one-time setup has done or not.

Save your other data in SharedPreference or SQLiteDatabase and retrieve those data in the GameChooseActivity.

public void startOneTimeSetUp(){

    if(!TextUtils.isEmpty(editUsername.getText().toString().trim())) {
                uploadUserData();
                Intent intent = new Intent(SplashActivity.this, GameChooseActivity.class);

                SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
                Editor editor = pref.edit();
                editor.putBoolean("hasDoneSetup", true);           
                editor.commit(); 

                finish();
            }
}


public boolean checkForSetUp(){

     return pref.getBoolean("hasDoneSetup", true);
}

public void startGameChooseActivity(){
     Intent intent = new Intent(SplashActivity.this, GameChooseActivity.class);
     startActivity(intent);
}

Relevant links

If you don't know What is SharedPreference?
If you don't know how to use SharedPreference check this

Upvotes: 0

Seyed Masood Khademi
Seyed Masood Khademi

Reputation: 173

You can use this code:

Intent intent=new Intent(NewUserActivity.this, GameChooseActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
    NewUserActivity.this.finish();

Upvotes: 0

Related Questions