简体   繁体   中英

Application called from the exited activity

I've used the following code in my application to exit from it when the user clicks the button. It is working. But the problem is, when I open the application again, it will not be started from the main activity. Instead, it will be started from the activity where i exited.

Intent i=new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
FarmerDetails.this.startActivity(i);
finish();

Full code:

public class FarmerDetails extends Activity {
Button ok,old,ok1,exit;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.labour); 
    ok=(Button)findViewById(R.id.button1);
    ok1=(Button)findViewById(R.id.button2);
    exit=(Button)findViewById(R.id.btn_exit);
    exit.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {

            Intent i=new Intent();
            i.setAction(Intent.ACTION_MAIN);
            i.addCategory(Intent.CATEGORY_HOME);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            FarmerDetails.this.startActivity(i);
            finish();

        }

    });
     ok1.setOnClickListener(new OnClickListener() {

         public void onClick(final View v) {
             Intent next=new Intent(FarmerDetails.this,Fetch.class);
             startActivity(next);




         }
     });   



     ok.setOnClickListener(new OnClickListener() {

         public void onClick(final View v) {
             Intent next=new Intent(FarmerDetails.this,newfarmer1.class);
             startActivity(next);




         }
     });   



}
public void onDestroy()
{
    System.gc();
    android.os.Process.killProcess(android.os.Process.myPid());
    super.onDestroy();
}

}

Can anyone tell what is the problem and how to avoid?

I need to start the application from the main activity after exited.

I've found the answer. When the user pressed the exit button it loads the first Activity that runs when your app starts, in my case "main".

Intent i = new Intent(getApplicationContext(), main.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", true);
startActivity(i);

The above code clears all the activities except for main. main is the first activity that is brought up when the user runs the program.

Then put this code inside the main's onCreate, to signal when it should self destruct when the 'Exit' message is passed.

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();
}

在启动此活动B时,以及在调用finish的setResult(someint)和A的onActivityResult之前(如果resultcode == someint完成活动A等),请使用callerActivity A中的startActivityForResult。您需要销毁主要活动以从main重新启动活动

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM