简体   繁体   English

从首次启动android应用程序时的不同活动开始

[英]Start with different activity on first launch of android app

Is there a way to launch a different activity on startup once only? 有没有一种方法可以在启动时启动一次不同的活动? If I instantly launch my setup activity from my main activity, there is a 1 second pause with a white screen. 如果我从主活动中立即启动设置活动,则会有1秒钟的暂停并出现白屏。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent myIntent = new Intent(this, home2.class);
        this.startActivity(myIntent);
        finish();
        ...

    }

That can be done in several ways. 这可以通过几种方式来完成。 One of them is usage of the shared preferences where will be stored data about accessed activity (for example intro activity). 其中之一是共享首选项的使用,其中将存储有关所访问活动(例如介绍活动)的数据。

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean isAccessed = prefs.getBoolean(getString(R.string.is_accessed), false);
if(!isAccessed) {
    SharedPreferences.Editor edit = prefs.edit();
    edit.putBoolean(getString(R.string.is_accessed), Boolean.TRUE);
    edit.commit();
    showIntroActivity();
} else {
    startReqularActivity();
}

Also there is more ways to accomplish that request (for example -> storing accessing state in db, or properties file, or storing on the cloud if application have to be controlled from some back office). 另外,还有更多方法可以完成该请求(例如->将访问状态存储在db或属性文件中,或者如果必须从某个后台控制应用程序,则将其存储在云中)。 IMO this is the best way for achieving that functionality - and of course the simplest. IMO这是实现该功能的最佳方法-当然也是最简单的。

This is only idea (which is fully functional) and you can adapt it for your needs. 这只是个主意(功能齐全),您可以根据需要进行调整。

There is a solution described in the book Android Programming Pushing the Limits by Erik Hellman. Erik Hellman在《 Android编程突破极限》一书中描述了一种解决方案。 You need to do the following: 您需要执行以下操作:

  1. Create your activities (well, let's just say they are main and setup activities) and add them to your manifest like that: 创建您的活动(好吧,让我们说它们是主要活动和设置活动),然后将它们添加到清单中,如下所示:

     <activity android:name=”.SetupActivity” android:label=”@string/app_name_setup” android:icon=”@drawable/app_setup_icon” android:enabled=”true”> <intent-filter> <action android:name=”android.intent.action.MAIN”/> <category android:name=”android.intent.category.LAUNCHER”/> </intent-filter> </activity> <activity android:name=”.MainActivity” android:label=”@string/app_name” android:icon=”@string/app_icon” android:enabled=”false”> <intent-filter> <action android:name=”android.intent.action.MAIN”/> <category android:name=”android.intent.category.LAUNCHER”/> </intent-filter> </activity> 

  2. When Setup Activity is shown, disable it and enable Main Activity programmatically: 显示设置活动时,将其禁用并以编程方式启用“主要活动”:

     PackageManager packageManager = getPackageManager(); packageManager .setComponentEnabledSetting(new ComponentName(this, MainActivity.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); packageManager .setComponentEnabledSetting(new ComponentName(this, SetupActivity.class), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 

I haven't tried this yet, but it still should work just fine. 我还没有尝试过,但是仍然可以正常工作。

I think that your original code is very close to what you want. 我认为您的原始代码非常接近您想要的代码。 There are a couple of things that you need to do to make it work visually, though. 但是,需要做几件事才能使其在视觉上起作用。

First, only call setContentView if you actually intend to show the MainActivity. 首先,仅在实际打算显示MainActivity时才调用setContentView。 Second, make sure that the SetupActivity loads quickly to avoid the delay. 其次,请确保快速加载SetupActivity以避免延迟。 That means making sure that you don't do anything time-consuming in onCreate. 这意味着确保您在onCreate中不做任何耗时的事情。

So, then, you can change your code for MainActivity to: 因此,您可以将MainActivity的代码更改为:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(isFirstTime()) {
           startActivity(new Intent(this, SetupActivity.class));
           finish();
        } else {
           setContentView(R.layout.activity_main);
           ... // anything else that you need to do to initialize MainActivity
        }
    }

尝试将这段代码放在setContentView()之前

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM