如何从启动页面过渡到主要活动?

问题描述 投票:0回答:4

当前,我的应用会加载启动屏幕并保持原样。我认为我的主要问题是Android生命周期。我将启动页面作为onCreate,并且主要活动(从下面的private final开始)也是onCreate,但是我认为这是不正确的。而且,每个活动都在一个Java文件中。它是否正确?如何加载我的启动页面,杀死自己,然后加载我的主要活动?

这是我的MainActivity.java:

package com.deliveryfor;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;

public class MainActivity extends Activity {

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.splash); 

        Thread Timer = new Thread() { 
            public void run() { 
                try { sleep(3000); 

                startActivity (new Intent ("android.intent.action.SPLASH")); 


                } catch (InterruptedException e) { 
                    e.printStackTrace(); 

                } 

                finally { 

                    finish(); 

                } 

            } 

        }; 

        Timer.start(); 

    }

    private final LatLng LOCATION_STATE = new LatLng(49.27645, -122.917587);
    private final LatLng LOCATION_CITY = new LatLng(49.187500, -122.849000);
    private final LatLng LOCATION_YOU = new LatLng(49.187500, -122.849000);

    private GoogleMap map;



    protected void onCreate() {
        super.onCreate(null);
        setContentView(R.layout.activity_main);

        map  = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        map.setMyLocationEnabled(true);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onClick_State(View v) {
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_YOU, 9);
        map.animateCamera(update);
    }
    public void onClick_City(View v) {
        map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_CITY, 16);
        map.animateCamera(update);

    }
    public void onClick_You(View v) {
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_STATE, 18);
        map.animateCamera(update);

    }

}

这是Menu.java(初始页面):

package com.deliveryfor;

import android.app.Activity;
import android.os.Bundle;

public class Menu extends Activity { 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView (R.layout.splash); }




    }
java android eclipse splash-screen android-lifecycle
4个回答
2
投票

您必须将Splash Activity创建为经典活动,并且必须在Manifest中声明它将是第一个启动的活动。将此行添加到活动初始标记中:

<intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

然后,在SplashActivity.java的onCreate()中,添加自动启动MainActivity的代码。我像这样使用TimerTask:

Timer timer = new Timer();
        timer.schedule(new TimerTask(){

            @Override
            public void run() {
                // TODO Auto-generated method stub

                Intent toHomePage = new Intent(context,MainActivity.class);
                startActivity(toHomePage);
                finish();
            }}, 5000);

5000表示将在5秒钟后启动Intent toHomePage。希望对您有帮助!


1
投票

您需要创建一个Intent来启动MainActivity。现在,Menu的代码除了创建其UI外没有做任何其他事情。 official documentation描述了如何执行此操作。

您将需要这样的东西:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

除非您有令人信服的理由(似乎并非如此),否则您可能还想重新考虑使用启动画面。有关详细信息,请阅读this blog帖子。这是我觉得很有趣的摘录:

启动画面可用于在 应用程序启动。我个人认为98%的人没有必要 案件。对于主动依赖重载的应用程序可能很有用 资源,例如Google Earth,Sky Map或游戏,但这不是 适用于简单的实用程序应用程序,例如提要阅读器,社交 网络应用,新闻阅读器等。您不需要网络 在启动时也不进行连接。


1
投票

我这样使用它:

public class SplashActivity extends Activity{
    private final static long SPLASH_TIME_OUT = 2000;

    // SplashActivity --> serves as entry-point for the app at several situations
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_splash);                      
         new Handler().postDelayed(new Runnable() {

              @Override
              public void run() {

                Intent i = new Intent(SplashActivity.this, RegisterActivity.class);
                startActivity(i);
                // close this activity
                finish();
              }
        }, SPLASH_TIME_OUT);
    }   
}

0
投票

我将此活动作为MainActivity.java添加到第一页:

public class MainActivity extends AppCompatActivity {
    oAuth_Model oAuth_model;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        showBasicSplash();
    }
    private void showBasicSplash() {
        new Thread(){
            @Override
            public void run() {
                super.run();

                try {
                    Thread.sleep(1500);
                    handler.sendEmptyMessage(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){
        public void handleMessage(Message m){
            if (getBoolean(Constant.AFTERLANG)){
                Intent intent = new Intent(MainActivity.this, Landing_three.class);
                startActivity(intent);
                finish();
            }else {
                Intent intent = new Intent(MainActivity.this, Activity_Sec.class);
                startActivity(intent);
                finish();
            }
        }
    };
}

并将此活动添加到Manifests.xml中:

<activity android:name=".MainActivity"
        android:configChanges="orientation"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

并在打算进行其他活动后杀死Mainactivity。

finish();
© www.soinside.com 2019 - 2024. All rights reserved.