我在我的应用程序中添加了启动画面,我的代码如下所示:
public class SplashActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000;//for testing i use 5 seconds
private Handler handler = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
}, DELAY_MILLIS);
}
@Override
protected void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
}
我的宣言:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vdovin.currencyratesapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".application.CurrencyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".screens.splash.SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".screens.main.CurrencyExchangeActivity">
</activity>
</application>
</manifest>
所以我遇到了下一个问题:
如果我在加载启动画面时使用主页按钮隐藏我的应用程序,那么当我再次打开应用程序时,启动屏幕活动不会调用CurrencyExchangeActivity
。我知道它出现是因为方法onCreate()
仅调用一次,但我无法将其放入onResume()
,因为当我再次打开我的应用时,它再次向我显示启动画面。但我想展示CurrencyActivity
,就像谷歌的应用程序(地图,工作表等......)
答案 0 :(得分:0)
请尝试这种方式。我还没有测试过您的代码。我对使用启动的影响如下:
public class SplashActivity extends Activity {
private static final String TAG = SplashActivity.class.getSimpleName();
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
loadNext();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Thread() {
@Override
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(1);
}
}.start();
}
protected void loadNext() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
}
}
答案 1 :(得分:0)
你忘了添加
setContentView(R.layout.splash);
添加它将起作用
答案 2 :(得分:0)
也许您可以尝试以下方法:
@Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
按下主页按钮后重新打开应用程序时会跳过此延迟。
答案 3 :(得分:0)
我通常使用postDelayed Runnable。
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
@Override
public void run() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
// 5 sec
handler.postDelayed(runnable, 5000);
}
@Override
protected void onPause() {
super.onPause();
// 5 sec
handler.removeCallbacks(runnable);
}
}
答案 4 :(得分:0)
您不能这样做只是因为您将启动画面图像设置为background
theme
的{{1}}。这是窗口级别的背景,无论您的导航或任何东西都会出现。
如果你真的想在应用生命周期中只显示一次启动画面,那么你必须在Activity
中执行此操作并将该图像作为布局的一部分,并使用not the right way
来扩充该布局。现在当你再次进入应用时,你会在setContentView
CurrencyExchangeActivity
setContentView
之前调用splash activity
,这只会显示黑色窗口背景并直接显示CurrencyExchangeActivity
。
让我知道这是否有意义。如果需要,我可以详细说明
答案 5 :(得分:0)
我建议在您的案例中保持启动屏幕显示机制CurrencyActivity
。这是伪代码。
我在代码中添加了适当的注释。请检查。
public class CurrencyActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000; //for testing i use 5 seconds
private Handler handler = null;
private boolean showSplash = true; // True by default
private ImageView splashImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currency_activity);
// Get a image overlaying the other views in your currency layout
splashImage = (ImageView) findViewById(R.id.splash);
if(showSplash) showSplashScreen();
}
@Override
protected void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
@Override
protected void onPause() {
super.onPause();
showSplash = false; // Set the variable to false when you take this in background
}
@Override
protected void onResume() {
super.onResume();
// Check if the variable is false and then set the visibility to GONE
if(!showSplash) splashImage.setVisibility(View.GONE);
}
private void showSplashScreen() {
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
splashImage.setVisibility(View.GONE);
showSplash = false;
}
}, DELAY_MILLIS);
}
}
答案 6 :(得分:0)
试试这个 -
SplashScreenActivity -
public class SplashActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000;//for testing i use 5 seconds
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
}, DELAY_MILLIS);
}
}
对于清单 -
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CurrencyExchangeActivity"/>
</application>
我测试了这段代码,如果您遇到的问题是当按下主屏幕按钮时会再次显示启动画面,那么这将有效....