微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过单击应用程序图标打开Android应用程序时重新启动

我是 Android开发世界的新手,我已经建立了一个简单的“Hello World”应用程序.首先,活动请求一个文本.当单击“Go”按钮时,应用程序将启动显示输入文本的第二个活动.

如果我单击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>

解决方法

问题:

我没有资格说这个错误,但是从启动器启动应用程序时,有一个发布版本的行为.看来,它不是恢复上一个Activity,而是在顶部添加一个新的Activity.有一个关于这个话题的相关错误报告here.

解:

如果不是任务的根,关闭启动器活动,我正在解决这个问题,因此该任务中的上一个Activity将会被恢复.

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

原文地址:https://www.jb51.cc/android/313099.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐