点击应用程序图标打开时,Android应用程序重新启动

15

我刚接触Android开发,已经制作了一个简单的“Hello World”应用程序。首先,活动请求文本。当点击“Go”按钮时,应用程序启动第二个活动,显示输入文本。

如果我点击HOME按钮,然后点击应用程序图标,应用程序会再次启动第一个活动,但是如果我按住Home按钮并从“最近使用的应用程序”栏中点击图标,则会恢复到离开时的应用程序状态。

我该如何避免这种情况?

无论点击哪个启动器图标,我都需要我的应用程序恢复。

MainActivity.java,

package com.example.myfirstandroidapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
  public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
  /** Called when the user clicks the Send button */
  public void sendMessage(View view){
    // Do something in response to button
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.txtName);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
  }
}

DisplayActivity.java

package com.example.myfirstandroidapp;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);

    // Show the Up button in the action bar.
    setupActionBar();
  }

  /**
   * Set up the {@link android.app.ActionBar}, if the API is available.
   */
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      getActionBar().setDisplayHomeAsUpEnabled(true);
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display_message, menu);
    return true;
  }

  @Override
  public void onDestroy(){
    super.onDestroy();
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
      // This ID represents the Home or Up button. In the case of this
      // activity, the Up button is shown. Use NavUtils to allow users
      // to navigate up one level in the application structure. For
      // more details, see the Navigation pattern on Android Design:
      //
      // http://developer.android.com/design/patterns/navigation.html#up-vs-back
      //
      NavUtils.navigateUpFromSameTask(this);
      return true;
    }
    return super.onOptionsItemSelected(item);
  }


}

activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <EditText
            android:id="@+id/txtName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="70dp"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/btnGo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/txtName"
            android:layout_alignParentRight="true"
            android:onClick="sendMessage"
            android:text="Go!" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/txtName"
            android:layout_alignParentTop="true"
            android:layout_marginTop="18dp"
            android:text="Please input your name:"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

activity_display_message.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".DisplayMessageActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />



</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstandroidapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myfirstandroidapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.myfirstandroidapp.DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.myfirstandroidapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstandroidapp.MainActivity" />
        </activity>
    </application>

</manifest>

当您点击主页按钮时,您的活动将被暂停。在后台暂停的活动可能会被系统终止以回收内存。但有时它不会被销毁,可以恢复。您可以覆盖生命周期方法并记录一些消息,以便自己了解情况。 - Raghunandan
我在我的设备上无法模拟相同的错误。你尝试过在第二个<activity>中添加android:finishOnTaskLaunch="false"吗?也许你使用的ROM已经被修改了...只是猜测。 - Hugo Hideki Yamashita
1
@HugoHidekiYamashita 当您使用IDE进行调试时,您无法看到这种情况发生。您必须从Play商店或apk安装才能重现此问题。 - Jorge Obregón
参考:当安装apk后,点击“打开”按钮,尝试导航到其他活动,然后按Home键暂停应用程序并单击启动器图标将刷新到第一个活动时,会发生此错误。 - 林果皞
2个回答

34

问题:

我不能说这是一个错误,但在从启动器启动应用程序时,发布版本存在一种行为。似乎它不是恢复之前的Activity,而是添加了一个新的Activity在其上方。有关此主题的相关错误报告 在此

解决方案:

我通过关闭启动器Activity(如果它不是任务的根目录)来解决这个问题,因此将恢复该任务中的上一个Activity。

if (!isTaskRoot()) {
    finish();
    return;
}

0

对我来说问题是当应用程序通过apk安装并点击“打开”选项时,每次最小化时它都会重新启动。

解决方法:

SplashActivity.java:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (!isTaskRoot
            && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
            && intent.action != null
            && intent.action.equals(Intent.ACTION_MAIN)) {
            finish()
            return
        }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接