0

这是我的想法,我想在 android 中创建一个类似活动的组件,所以以这种方式,我希望我的主要活动源自我的启动活动,该启动活动源自 Activity,并且在我的启动活动中,我想检查许可证,如果它是可用然后我想重定向到从启动活动派生的主要活动。

我的整个想法是拥有一个组件,因此通过从主要活动扩展它,一切都应该自动化。

我已经使用以下代码完成了此操作,但它运行不顺利,我确信我的方法有问题:

//my main activity which will be shown after splash screen
public class MyActivity extends SplashLoader
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.setNextActivity(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText("hello");

    }
}

//my splash activity

public class SplashLoader extends Activity
{
    private static final String ERROR_MESSAGE = "Error message";
    private  Activity nextActivity=null;

    public Activity getNextActivity() {
        return nextActivity;
    }

    public void setNextActivity(Activity nextActivity) {
        this.nextActivity = nextActivity;
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        LinearLayout lLayout = new LinearLayout(this);
        lLayout.setOrientation(LinearLayout.VERTICAL);
        lLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ImageView iv = new ImageView(this);
        iv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        iv.setImageResource(R.drawable.logo);
        lLayout.addView(iv);
        setContentView(lLayout);

boolean isRegistered = false;
        if(!isRegistered)
        {
            createDialog().show();
        }
        else
        {
        //    finish();
            Intent intent = new Intent(SplashLoader.this,getNextActivity().getClass());
            startActivity(intent);

        }
    }



    private AlertDialog.Builder createDialog()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);


        DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which)
            {
                switch (which)
                {
                    case DialogInterface.BUTTON_NEUTRAL:
                        System.exit(0);
                        break;
                    default:
                        break;
                }

            }
        } ;

        builder.setMessage(ERROR_MESSAGE).setNeutralButton("Ok" ,clickListener );
        return builder;
    }
}

//and my manifest 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application android:label="@string/app_name">
        <activity android:name="MyActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

编辑:

这种方法没有任何错误,但是在splash想要加载mainActivity之后它有一个滞后或者在改变方向之后我可以看到mainActivity。这种方法的行为有点奇怪。

4

1 回答 1

0

MainActivity.onCreate()你打电话super.onCreate()。因为MainActivitySplashLoaderthis 派生的最终会调用SplashLoader.onCreate(). 在SplashLoader.onCreate()你膨胀视图并调用 setContentView()。然后MainActivity.onCreate()你再做一次(从而覆盖你所做的所有工作SplashLoader.onCreate())。膨胀视图需要时间(和资源),这可能会产生部分可见延迟。此外,SplashLoader.onCreate()调用 startActivity() 来启动MainActivity,这将在MainActivity.onCreate() calls super.onCreate().

如果你真的想做一些事情,比如MainActivity从扩展SplashLoader,那么你需要修复你的生命周期覆盖的工作方式。您可能需要执行以下操作:

SplashLoader中:

@Override
public void onCreate(Bundle savedInstanceState)
{
    setNextActivity(this);
    super.onCreate(savedInstanceState);
    // Do any activity-specific stuff
    doOncreate(savedInstanceState);
}

// Derived classes should NOT override onCreate(), but should
//  instead override doOnCreate() and put their activity-specific code there
protected void doOnCreate(Bundle savedInstanceState) {
}
    // This is the SplashLoader-specific onCreate() stuff
    LinearLayout lLayout = new LinearLayout(this);
    lLayout.setOrientation(LinearLayout.VERTICAL);
    ... etc etc etc ...
}

MainActivity

@Override
protected void doOnCreate(Bundle savedInstanceState)
{
    // This is the MainActivity-specific onCreate() stuff
    setContentView(R.layout.main);
    TextView tv = (TextView) findViewById(R.id.tv);
    tv.setText("hello");
}
于 2012-07-07T11:09:21.040 回答