简体   繁体   English

如何在运行应用程序时使其在每次启动时都干净启动?

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

This is my onCreate in MainActivity.java 这是我在MainActivity.java中的onCreate

@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. initTTS用于使用文本进行语音转换。

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. 问题是,当我通过android-studio一遍又一遍地运行该应用程序时,该应用程序将开始清理/重置。 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. 但是,当我在这种情况下在lg g3上一次又一次地在我的android设备上运行该应用程序时,我单击了该应用程序图标,因此每次它记住上一次操作时,有时只是一个TextView文本发生了变化,或者有时是文本变为了语音,所以有时我会看到更改后的文本,有时我会听到最后一个语音。 Like it's remembering the last actions. 就像记住最后的动作一样。

I tried an hour ago to add this to my MainActivitiy.java 我在一小时前尝试将其添加到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. 但是MainScreen不存在,所以我不确定应该添加什么而不是它。 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 . 当您按下主屏幕按钮时,应用程序从Resumed(可见)移动到暂停(部分可见),直到android需要另一个应用程序的内存/资源,然后移动到Stopped并销毁,这表示您不是用户友好或“ android方式”。

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" 将此添加到清单文件到您希望每次清除的活动中: android:noHistory="true"
and override your on back press : 并覆盖您的背部新闻:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java for iOS中使用高跟鞋类进行的每项测试之前,如何使Appium和应用程序自动启动? - How can I make Appium and the app launch automatically before every test in Java for iOS with heels classes? 当应用程序在后台或被杀死时,如何在收到通知时启动我的应用程序或活动? - how can i launch my app or an activity on notification received when app is in background or killed? ¿ 如何在后台启动我的应用程序或活动? - ¿How can i launch my app or an activity in background? 如何在 vscode 中设置 launch.json 中的默认 java 配置,以使 vscode 在每次运行应用程序时停止要求我指定主类? - How can I set in vscode a default java configuration in launch.json to make vscode stop asking me to specify a main class every time I run the app? 如果玩家键入 /launch,我如何让我的代码向玩家返回“无效号码”消息<notaninteger>或 /launch 字符串</notaninteger> - How can I make my code return a “Invalid number” message to a player if they type /launch <notaninteger> or /launch String 如何完全清洁面板 - how can I clean my panel completely 如何在执行eclipse Project clean时启动ant clean目标 - How to launch ant clean target when executing eclipse Project clean 如何在启动 android 应用程序时启动特定片段 - How can I launch specific fragment at launch android app 如何让我的应用程序读取我的联系人列表? - How can I make my app read my contact list? 尝试使用ASyncTask发出GET请求时,如何防止我的应用程序崩溃? - How can I stop my app from crashing when trying to make a GET request with an ASyncTask?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM