简体   繁体   English

Android:控制权返回初始画面

[英]Android: Control goes back to Splash Screen

I have designed an app with a splash screen that sleep() s for 3 seconds and displays the Home screen of my app. 我设计了一个带有启动屏幕的应用程序,该应用程序的sleep()持续3秒钟,并显示我的应用程序的主屏幕。 I can navigate into my app seamlessly and after i come back to Home screen, when Back button is pressed the control goes back to Splash screen again instead of terminating the app. 我可以无缝导航到我的应用程序,然后返回主屏幕后,当按下“返回”按钮时,控件将再次返回到“启动”屏幕,而不是终止应用程序。 Please give me a solution. 请给我一个解决方案。 :) :)

Since you've not posted the code, I am guessing the you did not call finish(); 由于您尚未发布代码,所以我猜您没有调用finish(); . You may call it inside your onPause() or before calling the new Intent as other's have suggested. 您可以在onPause()内部调用它,也可以按照其他人的建议在调用新的Intent之前调用它。

Update 更新资料

If you are just loading a splash screen, you might just set the parameter to not keep it in activity stack. 如果仅加载初始屏幕,则可以将参数设置为不将其保留在活动堆栈中。 In your manifest.xml, where you define your activity do: 在清单文件中定义活动的位置:

<activity android:name=".SplashScreen" android:noHistory="true" ... />

You won't require to class finish() . 您不需要对finish()进行分类。 Just normally call startActivity() . 通常只需调用startActivity()

See: How to finish current activity in Android 请参阅: 如何在Android中完成当前活动

Calling finish() After Starting a New Activity 开始新活动后调用finish()

Start new Activity and finish current one in Android? 开始新的活动并在Android中完成当前活动?

Hope this helps. 希望这可以帮助。

Finish your splash activity before starting the new one. 在开始新的飞溅活动之前,先完成飞溅活动。 The onResume method of your splash activity could be something like this: 您的启动活动的onResume方法可能是这样的:

@Override
protected void onResume() {
    super.onResume();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            // finish the splash activity so it can't be returned to
            SplashActivity.this.finish();
            // create an Intent that will start the second activity
            Intent mainIntent = new Intent(SplashActivity.this, SecondActivity.class);
            SplashActivity.this.startActivity(mainIntent);
        }
    }, 3000); // 3000 milliseconds
}

Change your splash activity xml as below by adding android : noHistory =" true " and you are done. 通过添加android ,如下所示更改您的启动活动xml: noHistory =“ true ”,您就完成了。

<activity
        android:name="com.naveen.example.SplashActivity"
        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>

Before you start or let's say go to new Activity, finish old activity, like: 在您开始或说进入新的活动之前,请完成旧的活动,例如:

YourSplashActivity.this.finish();
Intent intent = new Intent(SplashActivity.this, SecondActivity.class);
YourSplashActivity.this.startActivity(intent);

In addition you can do followed logic: 另外,您可以遵循以下逻辑:

    // drop application to home activity (to prevent to show Splash)
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Assuming that your splash screen will only appear once when your application starts and will not show again until you have restarted your app after the app has finished running(meaning exiting the app by pressing the home button does not count), why not call finish() on overridden onPause() of your splash screen activity like the following. 假设启动屏幕仅在应用程序启动时出现一次,并且在应用程序完成运行后重新启动应用程序之前不会再次显示(这意味着通过按下主屏幕按钮退出应用程序不计数),为什么不调用finish( ),如下所示在您的启动画面活动的onPause()上进行覆盖。

@Override
public void onPause() {
    super.onPause();
    finish();
}

It's much simpler than running a thread or posting a Runnable object for the handler to handle. 这比运行线程或为处理程序发布一个Runnable对象要简单得多。 I have tested it and it seems to work. 我已经对其进行了测试,它似乎可以正常工作。 I don't know if there is any drawback to this solution but if there is any issue I overlooked I wish somebody would point it out for me. 我不知道此解决方案是否有任何缺点,但是如果有任何我忽略的问题,希望有人能为我指出。

Just for the record: With the solutions described here you're wasting time because they pause the initialization for 2-3seconds before they continue. 仅作记录:使用此处描述的解决方案,您在浪费时间,因为它们在继续之前将初始化暂停了2-3秒。

I prefer adding a Splash Screen Layout on top of my main_activity.xml . 我更喜欢在main_activity.xml顶部添加一个Splash Screen Layout I detect the first start of the application by extending Application. 我通过扩展Application来检测应用程序的首次启动。 If it´s the first start, I show my Splash-Screen while the UI is build in the background... (Use background threads if the ProgressBar lags!) 如果是第一次启动,我会在后台构建UI时显示我的启动屏幕...(如果ProgressBar滞后,请使用后台线程!)

//Extend Application to save the value. You could also use getter/setter for this instead of Shared Preferences...
public class YourApplication extends Application {

    public static final String YOUR_APP_STARTUP = "APP_FIRST_START";

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

        //set SharedPreference value to true
        SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putBoolean(YOUR_APP_STARTUP, true);
        editor.apply();     
        ...    
     }

Check for your first start in your MainActivity 检查您在MainActivity首次启动

public class YourMainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //hide actionbar and other menu which could overlay the splash screen
    getActionBar().hide();

    setContentView(R.layout.activity_main);

    Boolean firstStart = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(TVApplication.YOUR_APP_STARTUP, true);

    if (firstStart) {
        //First app start, show splash screen an hide it after 5000ms
        final RelativeLayout mSplashScreen = (RelativeLayout) findViewById(R.id.splash_screen);
        mSplashScreen.setVisibility(View.VISIBLE);
        mSplashScreen.setAlpha(1.0f);
        final FrameLayout mFrame = (FrameLayout) findViewById(R.id.frame_container);
        mFrame.setAlpha(0.0f);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
                        R.anim.fade_out_animation);
                fadeOutAnimation.setDuration(500);
                fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                        mFrame.setAlpha(1.0f);
                        getActionBar().show();
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        mSplashScreen.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                mSplashScreen.startAnimation(fadeOutAnimation);
            }
        }, 5000); //<-- time of Splash Screen shown

    } else {
        ((RelativeLayout) findViewById(R.id.splash_screen)).setVisibility(View.GONE);
        getActionBar().show();
    }

Insert the SplashScreen at top in your main.xml. 将SplashScreen插入main.xml的顶部。 I prefer RelativeLayout for that. 我更喜欢RelativeLayout In the example, SplashScreen is placed on to of a layout with the Navitgation Drawer , which we really love, don`t we? 在示例中,将SplashScreen放置在Navitgation Drawer的布局上,这是我们真正喜欢的,不是吗?

//main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <!-- The main content view -->

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- The navigation drawer list -->

        <ListView
            android:id="@+id/slider_list"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_gravity="start"
            android:background="@color/tvtv_background"
            android:choiceMode="singleChoice"
            android:divider="@drawable/nav_bar_divider"
            android:dividerHeight="1dp"
            android:listSelector="@android:color/transparent" />
    </android.support.v4.widget.DrawerLayout>

    <RelativeLayout
        android:id="@+id/splash_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:background="@color/tvtv_white"
        android:visibility="visible" >

        <ImageView
            android:id="@+id/splash_screen_logo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:paddingLeft="50dp"
            android:paddingRight="50dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/splash_screen_text"
            style="@style/TVTextBlueContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/splash_screen_logo"
            android:layout_centerHorizontal="true"
            android:padding="10dp"
            android:text="Awesome splash shiat" />

        <ProgressBar
            android:id="@+id/splash_screen_loader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/splash_screen_text"
            android:layout_centerHorizontal="true"
            android:clickable="false"
            android:indeterminate="true" />
    </RelativeLayout>

</RelativeLayout>   

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

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