-2

i have created two activity.xml,that is first.xml and second.xml and when user navigate to second.xml layout and when they pressed back button i want to totally kill the app that is when they again open it after back pressed they firstly see first.xml....but this doesnt happen.... THIS IS MY secondactivity.java

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
    public void onBackPressed() {

        moveTaskToBack(true);

    }
}
araknoid
  • 3,065
  • 5
  • 33
  • 35
Abid
  • 21
  • 4
  • This question has been asked before. Have you searched before asking? – Sufian May 30 '17 at 11:54
  • ya but not found suitable solution – Abid May 30 '17 at 15:47
  • Well, in such a situation you should list some links to those questions and state why they don't answer your question. As a result you will save yourself from answers which don't satisfy your criterion. You can do that now. – Sufian May 30 '17 at 15:57
  • ya thanks for telling actually i am beginner and just today created account,i am building my first app... :) – Abid May 30 '17 at 16:19

3 Answers3

1

I think you could find something by googling your issue. However, probably you need to do it the nasty way:

int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);

Besides, killing the app from the app itself may not be the best practice and you should leave the memory/process management to the OS as stated in the Android Application Lyfecycle:

https://developer.android.com/guide/components/activities/activity-lifecycle.html

This also means that your code may not work in future update of the OS where the application is supposed to run, if they decide that this is no longer allowed.

ChaSIem
  • 493
  • 1
  • 6
  • 18
  • Would `killProcess` clean up the JNI layer (if one is using C++ threads)? – IgorGanapolsky Oct 18 '17 at 20:29
  • 1
    At the end of the process, both Java and C++ resources should be marked as eligible to be freed. I am quite sure about that but I am still looking for some more specific documentation to add – ChaSIem Oct 23 '17 at 15:54
0
System.exit(0);

This will do that.

nullvoid
  • 121
  • 8
0

try adding when navigating to other activity :

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent);
finish(); // call this to finish the current activity

and then just call :

finish() in the other activity

Raut Darpan
  • 1,520
  • 9
  • 14