如何使用OOP而不是线程从启动画面显示另一个活动

时间:2012-07-07 09:50:51

标签: android android-activity

这是我的想法我想在android中创建一个类似活动的组件,所以这样我希望我的主要活动派生自我的泼水活动,这是从活动和我的启动活动中我想要检查许可证和如果它可用,那么我想重定向到从splash活动派生的主要活动。

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

我在代码之后使用了这个用途,但它运行不顺畅,我确信我的方法有问题:

//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。这种方法的行为有点特殊。

1 个答案:

答案 0 :(得分:0)

MainActivity.onCreate()中的

您正在呼叫super.onCreate()。由于MainActivity来自SplashLoader,因此最终会调用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 ...
}
主要活动

@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");
}