程序运行中按HOME键,然后再按桌面图标再次重新启动的问题

来源:互联网 发布:刷新页面的js 编辑:程序博客网 时间:2024/05/16 13:47

本文要说的问题如题,打开一个程序后,按home键切到桌面,然后在点击图标,回到的是程序的首页而不是刚才操作的界面,而如果首页是登录页,那么用户体验显然是相当不好的,另外,关于这样的一个问题,在程序没签名之前,从来没有出现过这样的问题,签名之后才出现这样的问题(亲自测试)

出现这个问题的原因应该是再次点击图标的时候activity的Oncreate()方法重新调用了,这时候我们判断他是否是点击图标再次打开的,如果是,直接finish掉

解决问题的代码如下:,只需要在主Activity的onCreate()方法中设置即可

 if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){            finish();            return;        }        setContentView(R.layout.activity_main);

本人测试的demo代码如下

package ss.tabhost.com.mybackstage;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;public class MainActivity extends AppCompatActivity {    String TAG = "debug";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Log.i(TAG, "onCreate: ");        //getIntent.getFlags()!=0||Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0        if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){            finish();            return;        }        setContentView(R.layout.activity_main);        findViewById(R.id.jump_two).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this,Main2Activity.class);                startActivity(intent);            }        });    }    @Override    protected void onStart() {        super.onStart();        Log.i(TAG, "onStart: ");    }    @Override    protected void onRestart() {        super.onRestart();        Log.i(TAG, "onRestart: ");    }    @Override    protected void onResume() {        super.onResume();        Log.i(TAG, "onResume: ");    }    @Override    protected void onPause() {        super.onPause();        Log.i(TAG, "onPause: ");    }    @Override    protected void onStop() {        super.onStop();        Log.i(TAG, "onStop: ");    }    @Override    protected void onDestroy() {        super.onDestroy();        Log.i(TAG, "onDestroy: ");    }}
对应的xml文件

<?xml version="1.0" encoding="utf-8"?><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"    tools:context="ss.tabhost.com.mybackstage.MainActivity">    <EditText        android:id="@+id/et"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:hint="请输入文字"        android:textSize="18sp" />    <Button        android:id="@+id/jump_two"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="跳转" /></RelativeLayout>
第二个Activity

package ss.tabhost.com.mybackstage;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class Main2Activity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        findViewById(R.id.jump_three).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(Main2Activity.this, Main3Activity.class);                startActivity(intent);            }        });    }}

xml

<?xml version="1.0" encoding="utf-8"?><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="ss.tabhost.com.mybackstage.Main2Activity">    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="这是Actity 2" />    <Button        android:id="@+id/jump_three"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="跳转" /></RelativeLayout>

第三个和第二个一样,就不贴了

本文参考资料如下:

http://blog.csdn.net/busjb/article/details/40891239


0 0
原创粉丝点击