简体   繁体   中英

How can i make my app when running it to start clean on every launch?

This is my onCreate in MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        
        startTime = SystemClock.uptimeMillis();
        serverChecksThread.start();
        status1 = (TextView) findViewById(R.id.textView3);
        timerValue = (TextView) findViewById(R.id.timerValue);
        uploadedfilescount = (TextView) findViewById(R.id.numberofuploadedFiles);
        uploadedfilescount.setText("Uploaded Files: 0");

        addListenerOnButton();
        initTTS();
    }

initTTS is for using text to speech.

public void initTTS() {
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    }

The problem is that when i'm running over and over again the application through android-studio the app start clean/reseted. But when i'm running the application over and over again on my android device in this case lg g3 i click on the app icon so each time it remember the last action sometimes it's just a TextView text that changed or sometimes the text to speech so sometimes i see the changed text and sometimes i hear the last voice speeched. Like it's remembering the last actions.

I tried an hour ago to add this to my MainActivitiy.java

public void onResume() {
        super.onResume();
        startActivity(new Intent(this, MainScree.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

But MainScreen not exist so i'm not sure what should i add instead of it. And also i'm not sure if this is the right way to do it at all.

What you are suggesting is not user friendly nor the "android way" when pressing the home button the App moves from Resumed(visible) to paused(partially visible) until android needs the memory/resources for another app then moves to Stopped and destroyed .

we can always use

 @Override
    public void onPuase() {
        finish();
    }

but this will cause you a lot of problems in the app flow later on . Android Activity生命周期

editted :: to achive the data cleaning without missing with flow

Add this to your manifest file to the actvity that you want to be cleared every time : android:noHistory="true"
and override your on back press :

@Override public void onBackPressed() { super.onBackPressed(); finish(); } 

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