How to exit from the application and show the home screen?

后端 未结 20 2048
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 09:45

I have an application where on the home page I have buttons for navigation through the application.

On that page I have a button \"EXIT\" which when clicked should t

相关标签:
20条回答
  • 2020-11-22 10:22

    first finish your application using method finish();

    and then add below lines in onDestroy for Removing Force close

    android.os.Process.killProcess(android.os.Process.myPid());
    super.onDestroy();
    
    0 讨论(0)
  • 2020-11-22 10:23

    If you want to end an activity you can simply call finish(). It is however bad practice to have an exit button on the screen.

    0 讨论(0)
  • 2020-11-22 10:23

    It is not recommended to exit your Android Application. See this question for more details.

    The user can always quit your app through the home button or in the first activity through the back button.

    0 讨论(0)
  • 2020-11-22 10:26

    Some Activities actually you don't want to open again when back button pressed such Splash Screen Activity, Welcome Screen Activity, Confirmation Windows. Actually you don't need this in activity stack. you can do this using=> open manifest.xml file and add a attribute

    android:noHistory="true"

    to these activities.

    <activity
        android:name="com.example.shoppingapp.AddNewItems"
        android:label="" 
        android:noHistory="true">
    </activity>
    

    OR

    Sometimes you want close the entire application in certain back button press. Here best practice is open up the home window instead of exiting application. For that you need to override onBackPressed() method. usually this method open up the top activity in the stack.

    @Override
    public void onBackPressed(){
    Intent a = new Intent(Intent.ACTION_MAIN);
    a.addCategory(Intent.CATEGORY_HOME);
    a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(a);
    
    }
    

    OR

    In back button pressed you want to exit that activity and also you also don't want to add this in activity stack. call finish() method inside onBackPressed() method. it will not make close the entire application. it will go for the previous activity in the stack.

    @Override
    public void onBackPressed() {
      finish();
    }
    
    0 讨论(0)
  • 2020-11-22 10:26

    I did it with observer mode.

    Observer interface

    public interface Observer {
    public void update(Subject subject);
    }
    

    Base Subject

    public class Subject {
    private List<Observer> observers = new ArrayList<Observer>();
    
    public void attach(Observer observer){
        observers.add(observer);
    }
    
    public void detach(Observer observer){
        observers.remove(observer);
    }
    
    protected void notifyObservers(){
        for(Observer observer : observers){
            observer.update(this);
        }
    }
    }
    

    Child Subject implements the exit method

    public class ApplicationSubject extends Subject {
    public void exit(){
        notifyObservers();
    }
    }
    

    MyApplication which your application should extends it

    public class MyApplication extends Application {
    
    private static ApplicationSubject applicationSubject;
    
    public ApplicationSubject getApplicationSubject() {
                if(applicationSubject == null) applicationSubject = new ApplicationSubject();
        return applicationSubject;
    }
    

    }

    Base Activity

    public abstract class BaseActivity extends Activity implements Observer {
    
    public MyApplication app;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        app = (MyApplication) this.getApplication();
        app.getApplicationSubject().attach(this);
    }
    
    @Override
    public void finish() {
        // TODO Auto-generated method stub
                app.getApplicationSubject().detach(this);
        super.finish();
    }
    
    /**
     * exit the app
     */
    public void close() {
        app.getApplicationSubject().exit();
    };
    
    @Override
    public void update(Subject subject) {
        // TODO Auto-generated method stub
        this.finish();
    }
    
    }
    

    let's test it

    public class ATestActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        close(); //invoke 'close'
    }
    }
    
    0 讨论(0)
  • 2020-11-22 10:27

    This works well for me.
    Close all the previous activities as follows:

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("Exit me", true);
        startActivity(intent);
        finish();
    

    Then in MainActivity onCreate() method add this to finish the MainActivity

        setContentView(R.layout.main_layout);
    
        if( getIntent().getBooleanExtra("Exit me", false)){
            finish();
            return; // add this to prevent from doing unnecessary stuffs
        }
    
    0 讨论(0)
提交回复
热议问题