简体   繁体   English

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

[英]How to transition from splash page to main activity?

Currently, my app loads the splash screen and stays that way.目前,我的应用程序加载启动画面并保持这种状态。 I think my main problem is with the Android life cycle.我认为我的主要问题是 Android 生命周期。 I have the splash page as the onCreate, and the main activity (which starts with private final, below) is also onCreate, but I don't think this is correct.我有启动页面作为 onCreate,主要活动(从私有 final 开始,如下)也是 onCreate,但我认为这是不正确的。 Also, each activity is in one java file.此外,每个活动都在一个 java 文件中。 Is this correct?这是正确的吗? How do I get my splash page to load, kill itself, then load my main activity?如何让我的启动页面加载,杀死自己,然后加载我的主要活动?

Here's my MainActivity.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);

    }

}

And here's the Menu.java (splash page):这是 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); }




    }

You have to create the Splash Activity as a classical activity, and you have to declare in the Manifest that it will be the first launched activity.您必须将 Splash Activity 创建为经典 Activity,并且必须在Manifest中声明它将是第一个启动的 Activity。 Add this line into the activity splash tag:将此行添加到活动启动标记中:

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

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

Then, in your SplashActivity.java , in the onCreate(), you add the code that automatically launch your MainActivity .然后,在SplashActivity.java的 onCreate() 中,添加自动启动MainActivity的代码。 I use a TimerTask like so:我使用 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 means that the Intent toHomePage is gonna be launched after 5 seconds. 5000表示 Intent toHomePage 将在 5 秒后启动。 Hope it helps ya!希望对你有帮助!

You need to create an Intent to launch your MainActivity.您需要创建一个 Intent 来启动您的 MainActivity。 Right now the code for Menu does nothing apart from creating its UI.现在,Menu 的代码除了创建它的 UI 之外什么都不做。 The official documentation describes how to do this. 官方文档描述了如何做到这一点。

You will need something like this :你将需要这样的东西:

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

You may also want to reconsider using a splash screen altogether, unless you have a compelling reason for it (which does not seem to be the case).您可能还想重新考虑完全使用闪屏,除非您有令人信服的理由(这似乎并非如此)。 Read this blog post for details.阅读这篇博文了解详情。 Here's an extract I find quite interesting :这是我觉得很有趣的一段摘录:

A splash screen can be used to make resources available before an application starts.启动画面可用于在应用程序启动之前使资源可用。 Personally I think it is not necessary in 98% of the cases.我个人认为在 98% 的情况下没有必要。 It may be useful for applications actively relying on heavy resources such as Google Earth, Sky Map, or games but this is not applicable to simple utility applications such as feed readers, social network apps, news readers, etc. You should not require a network connection at startup nor do heavy computations.它可能对主动依赖大量资源的应用程序有用,例如 Google Earth、Sky Map 或游戏,但这不适用于简单的实用程序应用程序,例如提要阅读器、社交网络应用程序、新闻阅读器等。您不应该需要网络启动时的连接也不做繁重的计算。

i use it like this:我像这样使用它:

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

I added this Activity to first page as MainActivity.java:我将此活动作为 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();
            }
        }
    };
}

And added this Activity to Manifests.xml :并将此活动添加到 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>

And kill Mainactivity after intent to other activities.并在意图其他活动后杀死 Mainactivity。

finish();

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

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