简体   繁体   English

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

[英]Android app restarts when opened by clicking app icon

Scenario :设想 :

I open my app by clicking icon, do something, navigate through activities, pause the app by clicking home button.我通过单击图标打开我的应用程序,执行某些操作,浏览活动,单击主页按钮暂停应用程序。

Case 1:情况1:

If I open my app by clicking icon again, the app restarts from the first activity.如果我再次单击图标打开我的应用程序,该应用程序将从第一个活动重新启动。

Case 2:案例2:

If I open my app from recently open apps (in 4.0 by pressing menu button and selecting my app) it starts from the paused state.如果我从最近打开的应用程序(在 4.0 中通过按菜单按钮并选择我的应用程序)打开我的应用程序,它会从暂停状态开始。

I want the behavior 2 always to occur, don't want my app to restart every time when it is opened by clicking icon.我希望行为 2 总是发生,不希望我的应用程序每次通过单击图标打开时都重新启动。

I have compared my manifest file with other apps and they are similar to mine, but behave differently (ie like 2nd case which i want).我已经将我的清单文件与其他应用程序进行了比较,它们与我的相似,但行为不同(即我想要的第二种情况)。

Edit:编辑:

This has been asked here : App completely restarting when launched by icon press in launcher这里有人问过: 通过启动器中的图标按下启动时,应用程序完全重新启动

but no answers :(但没有答案:(

I found it.我找到了。 I had set a flag android:launchMode="singleTask" in my activity flag .我在我的活动标志中设置了一个标志android:launchMode="singleTask" I deleted that code.我删除了那个代码。

I also added onsaveInstance method to all the activities in my code and it's working now!我还在代码中的所有活动中添加了onsaveInstance方法,现在它正在运行!

Thanks :)谢谢 :)

Add this to your launcher activity:将此添加到您的启动器活动中:

if (!isTaskRoot()) {
    finish();
    return; 
}
super.onCreate(savedInstanceState);

In current activity set some image which needs to be displayed for 2 seconds like below.在当前活动中设置一些需要显示 2 秒的图像,如下所示。

ImageView im = new ImageView(this);
im.setImageResource(set your image);
setContentView(im);
intentMainScreen = new Intent(getApplicationContext(), MainScreen.class);
Handler x = new Handler();
x.postDelayed(new splashhandler(), 2000);

Then start your activity in the SplashHandler class (which implements runnable and call start activity in run method).然后在 SplashHandler 类中启动你的活动(它实现了 runnable 并在 run 方法中调用 start 活动)。

It will display your Splash screen for 2 seconds and start another activity.它将显示您的初始屏幕 2 秒钟并开始另一个活动。

seems in AndroidManifest you kept your launch activity android:launchMode="singleTask" .似乎在 AndroidManifest 中,您保留了启动活动android:launchMode="singleTask" remove this one from your launch activity will solve problem从您的启动活动中删除这个将解决问题

Try to replace you splash Activity code with this code..尝试用此代码替换您的飞溅活动代码..

public class Splash extends Activity {

protected boolean _active = true;
protected int _splashTime = 2000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.splash);

    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while (_active && (waited < _splashTime)) {
                    sleep(100);
                    if (_active) {
                        waited += 100;
                    }
                }
            } catch (InterruptedException e) {
                e.toString();
            } finally {
                Intent intent = new Intent(getApplicationContext(),
                        MainActivity.class);
                startActivity(intent);
                finish();
            }
        }
    };

    splashTread.start();
}

@Override
protected void onPause() {
    super.onPause();
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
//      super.onBackPressed();
}
}

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

相关问题 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应用程序 - Android application restarts when opened by clicking the application icon 点击Android中的网址即可打开我的应用 - My app is opened when clicking on a URL in Android 当用户单击其图标时,Android应用程序将重新启动 - Android app restarts when user clicks on its icon 通过单击图标而不是通过 Play 商店打开时应用程序崩溃 - App crash when opened by clicking on icon, but not through Play Store Phonegap-单击图标重新启动应用程序,而不是切换到已经运行的应用程序 - Phonegap - clicking on icon restarts app instead of switching to already running app 单击应用程序图标时,android返回上次打开的活动 - android back to last opened activity when click on app icon 在Android上打开的应用程序视图中显示应用程序图标 - Show App Icon in View with Opened Apps on Android Sideview重新启动Android应用 - Sideview restarts Android app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM