如果通过单击应用程序图标,打开Android应用程序重新启动 [英] Android application restarts when opened by clicking the application icon

查看:190
本文介绍了如果通过单击应用程序图标,打开Android应用程序重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid开发世界,我已经建立了一个简单的Hello World应用程序。首先,活动要求的文本。当点击GO按钮,应用程序启动第二个活动显示输入文字。

如果我点击主页按钮,然后点击应用程序图标,应用程序再次启动的第一个活动,但如果我preSS-按住home键,然后单击图标,从最近的应用程序栏中,它恢复了应用程序在我离开。

我如何避免这种情况?

我需要即使启动器图标点击我的应用程序来恢复。

MainActivity.java,

 包com.example.myfirstandroidapp;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.View;
进口android.widget.EditText;

公共类MainActivity延伸活动{
  公共最后静态字符串EXTRA_MESSAGE =com.example.myfirstapp.MESSAGE;
  @覆盖
  保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
  }

  @覆盖
  公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
  }
  / **当用户点击发送按钮调用* /
  公共无效的sendMessage(查看视图){
    //做一些回应按钮
    意向意图=新的意图(这一点,DisplayMessageActivity.class);
    EditText上EDITTEXT =(EditText上)findViewById(R.id.txtName);
    字符串消息= editText.getText()的toString()。
    intent.putExtra(EXTRA_MESSAGE,消息);
    startActivity(意向);
  }
}
 

DisplayActivity.java,

 包com.example.myfirstandroidapp;

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

公共类DisplayMessageActivity延伸活动{

  @覆盖
  保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    //从意图消息
    意向意图= getIntent();
    字符串消息= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    //创建文本视图
    TextView中的TextView =新的TextView(本);
    textView.setTextSize(40);
    textView.setText(消息);

    //设置文本视图作为活动布局
    的setContentView(TextView的);

    //显示操作栏中的向上按钮。
    setupActionBar();
  }

  / **
   *设置{@link android.app.ActionBar},如果API是可用的。
   * /
  @TargetApi(Build.VERSION_ codeS.HONEYCOMB)
  私人无效setupActionBar(){
    如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
      getActionBar()setDisplayHomeAsUpEnabled(真)。
    }
  }

  @覆盖
  公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.display_message,菜单);
    返回true;
  }

  @覆盖
  公共无效的onDestroy(){
    super.onDestroy();
  }

  @覆盖
  公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
    案例android.R.id.home:
      //这个ID再presents主页或向上按钮。在这种情况下
      //活动,显示向上按钮。使用NavUtils允许用户
      //浏览在应用结构中的一个级别。对于
      //更多详细信息,请参阅在Android上设计导航模式:
      //
      // http://developer.android.com/design/patterns/navigation.html#up-vs-back
      //
      NavUtils.navigateUpFromSameTask(本);
      返回true;
    }
    返回super.onOptionsItemSelected(项目);
  }


}
 

activity_main.xml,

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:工具=htt​​p://schemas.android.com/tool​​s
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:paddingBottom会=@扪/ activity_vertical_margin
        机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
        机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
        机器人:paddingTop =@扪/ activity_vertical_margin
        工具:上下文=MainActivity。>

        <的EditText
            机器人:ID =@ + ID / txtName的
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginTop =70dp
            机器人:EMS =10>

            <不是requestFocus />
        < /的EditText>

        <按钮
            机器人:ID =@ + ID / btnGo
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignBottom =@ + ID / txtName的
            机器人:layout_alignParentRight =真
            机器人:的onClick =的sendMessage
            机器人:文本=去! />

        <的TextView
            机器人:ID =@ + ID / textView1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignLeft =@ + ID / txtName的
            机器人:layout_alignParentTop =真
            机器人:layout_marginTop =18dp
            机器人:文本=请输入您的姓名:
            机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    < / RelativeLayout的>
 

activity_display_message.xml,

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=DisplayMessageActivity。>

    <的TextView
        机器人:ID =@ + ID / textView2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/参考hello world/>



< / RelativeLayout的>
 

AndroidManifest.xml中,

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.myfirstandroidapp
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =9
        机器人:targetSdkVersion =10/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名称=com.example.myfirstandroidapp.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名称=com.example.myfirstandroidapp.DisplayMessageActivity
            机器人:标签=@字符串/ title_activity_display_message
            机器人:parentActivityName =com.example.myfirstandroidapp.MainActivity>
            &所述;元数据
                机器人:名称=android.support.PARENT_ACTIVITY
                机器人:值=com.example.myfirstandroidapp.MainActivity/>
        < /活性GT;
    < /用途>

< /舱单>
 

解决方案

问题:

我没有资格说这是一个错误,但自从第一款Android版本,这种行为已经出现。目前,在发布任务和roottasks一个问题开始从启动应用程序时生成。你可以在这里找到相关的bug报告

解决方法:

它可以是固定的加入以下code到你的onCreate你的发射活动:

 如果(!isTaskRoot()){
    完();
    返回;
}
 

I am new to the Android development world and I've built a simple "Hello World" app. 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.

How do I avoid this?

I need my app to resume even if the launcher icon is clicked.

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>

解决方案

Problem:

I'm not qualified to say this a bug, but this behaviour has been around since the first Android version. There is a problem with tasks and roottasks in release builds when starting the application from the launcher. You can find the related bug report here.

Solution:

It can be fixed adding following code to your onCreate of your launcher activity:

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

这篇关于如果通过单击应用程序图标,打开Android应用程序重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆