Skip to content Skip to sidebar Skip to footer

How To Kill Activity Permanetly After Button Click Using Shared Prefrance

public class MainActivity extends Activity { Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

Solution 1:

here is the complete solution

//firstly when you register the user set the shared preferences in your register class like this//declare pref editor 
            SharedPreferences prefs; 
        SharedPreferences.Editor prefsEditor;
            prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefsEditor = prefs.edit();

    //paste below peace of code when the registration will be success

                     prefsEditor.putString("register", "yes");
                 prefsEditor.commit(); 

//now in your first activity you just check the shared pref value to know the user is register or no 


 SharedPreferences prefs;
String register;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
        register=prefs.getString("register", "");
//now check the value of shared pref and apply the condition like this
Intent intent ;
    if(register.equalsIgnoreCase("yes"))
            {
                intent = newIntent(this, NextAct.class);
                startActivity(intent);
                finish();

            }
            else
            {
             intent = newIntent(this, Register.class);
                startActivity(intent);
                finish();
            }

Solution 2:

You cannot "kill" an activity but you can finish() it.

Solution 3:

In onCreate() create condition:

if (<your condition>) {
  startActivity(...);
  finish();
}

Solution 4:

Either finish the old activity when the new one is started:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         SharedPreferencesprefs= getSharedPreferences(LOC_PREF_FILE, MODE_PRIVATE);
     BooleanstartSecond= prefs.getBoolean("StartSecondActivty", false);

         if (startSecond) {
             Intentnewpage=newIntent(this, PhonrRegistaion.class);
             startActivity(newpage);
             finish();
        }

        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(newOnClickListener() {

                @OverridepublicvoidonClick(View arg0) {
                    // TODO Auto-generated method stub

                    Intent myintent=newIntent(this, nextActvity.class);

                    startActivities(null);
                    finish();

                    SharedPreferencesprefs= getSharedPreferences(
                LOC_PREF_FILE, MODE_PRIVATE);
                SharedPreferences.Editoreditor= prefs.edit();
                editor.putBoolean("StartSecondActivty", true);
                editor.commit();
                editor.apply();

                }
           });

    }
}

This is not permanent as such as it will reside in memory until GC.

If you want to permanently kill you activity (and app) try killing your own process:

// Kill everything you canpublicvoidkillMyProcess() {
        try {
            Processprocess= Runtime.getRuntime().exec(
                    "/system/bin/kill -9 -1");
            BufferedReaderreader=newBufferedReader(newInputStreamReader(
                    process.getInputStream()));
            int read;
            char[] buffer = newchar[4096];
            StringBufferoutput=newStringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();
            process.waitFor();
        } catch (IOException e) {
            thrownewRuntimeException(e);
        } catch (InterruptedException e) {
            thrownewRuntimeException(e);
        }
    }

Post a Comment for "How To Kill Activity Permanetly After Button Click Using Shared Prefrance"