有条件地设置第一项活动中的Andr​​oid [英] Conditionally set first activity in Android

查看:121
本文介绍了有条件地设置第一项活动中的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我们已经显示在我们的应用程序启动画面。但是,如果没有数据,以便收集和处理正在等待,我们想直接进入我们的第一个活动。有没有办法做到这一点,而不闪屏闪?

的启动画面部分的的Andr​​oidManifest.xml如下:

 <活动机器人:名称=com.example.SplashScreenActivity
        机器人:标签=@字符串/ APP_NAME
        机器人:noHistory =真正的>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>

            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
 

解决方案

我在过去成功地做的是创造一种无形的活动为主要活动。它永远不会被显示给用户,因为它推出的正确的活动在构造函数中。

里面我把一些逻辑决定显示给用户哪些活动第一。这完全适用于我的使用情况 - 试试看


舱单申报(注意 noHistory =真正的参数):

 <活动
        机器人:名称=。activity.EntryActivity
        机器人:launchMode =singleInstance
        机器人:noHistory =真正的>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>
            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
 

注意:正如在下面的评论中,launchmode设定是无关的这个问题IIRC。这是关系到该EntryActivity推出的各种方法。


EntryActivity类:

 公共类EntryActivity扩展活动
{

    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //推出了不同的活动
        意图launchIntent =新意图();
        类<> launchActivity;
        尝试
        {
            字符串的className = getScreenClassName();
            launchActivity =的Class.forName(类名);
        }
        赶上(ClassNotFoundException的E)
        {
            launchActivity = DefaultHomeActivity.class;
        }
        launchIntent.setClass(getApplicationContext(),launchActivity);
        startActivity(launchIntent);

        完();
    }

    活动展现/ **收益类名称** /
    私人字符串getScreenClassName()
    {
        //注意 - 将这里的逻辑,以确定哪些屏幕显示下一个
        //默认采用的是该演示code
        串活性= DefaultHomeActivity.class.getName();
        返回的活动;
    }

}
 

Currently we have a splash screen that is displayed in our app. However, if there is no data to be gathered or processed that is waiting, we'd like to go straight into our first activity. Is there a way to do that without having the splash screen flash?

The AndroidManifest.XML of the splashscreen portion is as follows:

<activity android:name="com.example.SplashScreenActivity"
        android:label="@string/app_name"
        android:noHistory="true" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

解决方案

What I have done successfully in the past is to create an invisible activity as the main activity. It never gets shown to the user, because it launches the "correct" activity in the constructor.

Inside I place some logic which determines which activity to show to the user first. This works perfectly for my use case - give it a try.


Manifest Declaration (note the noHistory="true" parameter):

    <activity
        android:name=".activity.EntryActivity"
        android:launchMode="singleInstance"
        android:noHistory="true" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Note: As pointed out in comments below, the launchmode setting is unrelated to this question IIRC. This was related to the various ways that the EntryActivity was launched.


EntryActivity class:

public class EntryActivity extends Activity 
{ 

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

        // launch a different activity 
        Intent launchIntent = new Intent(); 
        Class<?> launchActivity; 
        try
        { 
            String className = getScreenClassName(); 
            launchActivity = Class.forName(className); 
        } 
        catch (ClassNotFoundException e) 
        { 
            launchActivity = DefaultHomeActivity.class; 
        } 
        launchIntent.setClass(getApplicationContext(), launchActivity); 
        startActivity(launchIntent); 

        finish(); 
    } 

    /** return Class name of Activity to show **/
    private String getScreenClassName() 
    {       
        // NOTE - Place logic here to determine which screen to show next
        // Default is used in this demo code
        String activity = DefaultHomeActivity.class.getName(); 
        return activity; 
    } 

} 

这篇关于有条件地设置第一项活动中的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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