简体   繁体   English

通过单击应用程序图标打开Android应用程序

[英]Android application restarts when opened by clicking the application icon

I am new to the Android development world and I've built a simple "Hello World" app. 我是Android开发世界的新手,我已经构建了一个简单的“Hello World”应用程序。 First, activity requests a text. 首先,活动请求文本。 When the "Go" button is clicked, the app launches the second activity displaying the input text. 单击“开始”按钮后,应用程序将启动显示输入文本的第二个活动。

If I click the HOME button and then click the application icon, the app launches the first activity again but if I press-hold the home button and click the icon from the "Recent apps" bar, it resumes the app where I left. 如果我单击HOME按钮然后单击应用程序图标,应用程序将再次启动第一个活动,但如果我按住主页按钮并单击“最近的应用程序”栏中的图标,它将恢复我离开的应用程序。

How do I avoid this? 我该如何避免这种情况?

I need my app to resume even if the launcher icon is clicked. 即使单击了启动器图标,我也需要恢复应用程序。

MainActivity.java, 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, 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, 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, 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, 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>

Problem: 问题:

I'm not qualified to say this a bug, but there is a behaviour with release builds when starting the application from the launcher. 我没有资格说这是一个bug,但是从启动器启动应用程序时有一个发布版本的行为。 It seems that instead of resuming the previous Activity, it adds a new Activity on top. 似乎不是恢复之前的Activity,而是在顶部添加一个新的Activity。 There is a related bug report on this topic here . 有关于这一主题相关的bug报告在这里

Solution: 解:

I'm working around this this by closing the Launcher Activity if it's not the root of the task, as a result the previous Activity in that task will be resumed. 我正在通过关闭Launcher Activity来解决这个问题,如果它不是任务的根,那么该任务中的前一个Activity将被恢复。

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

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

相关问题 单击应用程序图标打开 Android 应用程序时会重新启动 - Android app restarts when opened by clicking app icon Android应用程序仅在单击应用程序图标时才会从启动画面重新启动 - Android app restarts from splash screen when opened by clicking app icon only First time 通过单击应用程序图标(来自后台)打开本机 android 应用程序重新启动 - react native android app restarts when opened by clicking app icon (coming from background) 如果从文件夹中打开后单击主屏幕图标,Android应用程序将重新启动,反之亦然,但只有从Play Market下载后, - Android application restarts if homescreen icon clicked after opened from folder and vice versa but only if downloaded from Play Market 在SearchView中单击以重新启动应用程序 - Clicking inside SearchView restarts application Android:应用有时会重新启动 - Android: Application restarts sometimes 单击应用程序图标时,应用程序重新启动 - App relaunch when clicking on application icon Android服务重新启动,但应用程序未启动 - Android service restarts, but application does not Android 应用程序在方向更改时重新启动 - Android application restarts on orientation change 在InterstitialAd之后,Android应用程序重新启动 - android application restarts after InterstitialAd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM