2013-07-31 60 views
1

我设计了一个启动屏幕的应用程序,睡眠()3秒钟,并显示我的应用程序的主屏幕。我可以无缝地导航到我的应用程序,并且在我回到主屏幕后,当按下“返回”按钮时,控件会再次返回到闪屏,而不是终止应用程序。请给我一个解决方案。 :)Android:控制回到初始屏幕

+0

请显示从Spalsh活动开始“家庭”活动的代码。 –

回答

1

完成您的飞溅活动,然后再开始新的活动。你溅活动的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 
} 
7

既然你不发布的代码,我猜你也没有告诉finish();。你可以在你的onPause()之内或者在其他人建议的新的Intent之前调用它。

更新

如果你只是加载一个启动画面,你可能只是设置的参数不保存在活动堆栈。在你的manifest.xml,在那里你定义您的活动做:

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

您将不需要对finish()类。通常只需拨打startActivity()即可。

参见:How to finish current activity in Android

Calling finish() After Starting a New Activity

Start new Activity and finish current one in Android?

希望这有助于。

+0

这个答案应该被接受。 –

0

开始之前还是让我们说去新的活动,完成老活动,如:

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

另外,你可以做遵循的逻辑:

// 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); 
0

假设你的闪屏不仅会在应用程序启动后会出现一次,并且在应用程序运行完毕后重新启动应用程序(即通过按主屏幕按钮不计算退出应用程序)重新启动应用程序时不会显示,为什么不调用finish()覆盖onPause()您的启动画面活动如下所示。

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

这比运行线程或发布处理程序要处理的Runnable对象简单得多。我测试了它,它似乎工作。我不知道这个解决方案是否存在任何缺陷,但是如果我忽略了任何问题,我希望有人会为我指出。

2

加入的Android如下更改飞溅活动的xml:noHistory = “” 和你做。

<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> 
0

只是为了记录:对于此处描述你在浪费时间,因为他们暂停初始化2-3seconds他们继续之前的解决方案。

我更喜欢在我的main_activity.xml之上添加Splash Screen Layout。我通过扩展应用程序来检测应用程序的第一次启动。如果it's第一个开始,我告诉我的启动画面,而UI是建立在后台...(使用后台线程,如果进度落后!)你MainActivity

//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();  
     ...  
    } 

检查你的第一次启动

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

将SplashScreen插入main.xml中的顶部。我更喜欢RelativeLayout。在这个例子中,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>