启动画面 Activity
Splash Screen Activity
我在我的应用程序中添加了启动画面,我的代码如下所示:
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>
所以我遇到了下一个问题:
如果我在加载启动画面时使用主页按钮隐藏我的应用程序,那么当我再次打开应用程序时,启动画面 activity 不会调用 CurrencyExchangeActivity
。我知道它出现是因为方法 onCreate()
只调用了一次,但我不能把它放在 onResume()
中,因为当我再次打开我的应用程序时,它会再次显示启动画面。但我想显示 CurrencyActivity
,例如 google 的应用程序(地图、表格等...)
请试试这个方法。我没有测试过你的代码。我使用 splash 的方式如下:
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();
}
}
您忘记添加了
setContentView(R.layout.splash);
添加这个就可以了
也许您可以尝试以下方法:
@Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
这将跳过按主页按钮后重新打开应用程序时的延迟。
我通常使用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);
}
}
您不能仅仅因为将初始屏幕图像设置为 Activity
的 theme
的 background
就可以这样做。那是 window 级别的背景,无论您的导航或任何其他内容都会显示。
如果您真的想在应用程序生命周期中只显示一次启动画面,那么您必须在 not the right way
中执行此操作并将该图像作为布局的一部分并使用 [=14= 扩展该布局].现在,当您再次进入该应用程序时,您甚至会在 splash activity
的 setContentView
之前调用您的 CurrencyExchangeActivity
并且只会显示黑色 window 背景并直接显示 CurrencyExchangeActivity
.
让我知道这是否有意义。如果需要我可以详细说明
我建议在您的案例中保留 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);
}
}
试试这个--
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>
我测试了这段代码,如果你遇到按下主屏幕按钮时启动画面再次显示的问题,那么这就可以了....
我在我的应用程序中添加了启动画面,我的代码如下所示:
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>
所以我遇到了下一个问题:
如果我在加载启动画面时使用主页按钮隐藏我的应用程序,那么当我再次打开应用程序时,启动画面 activity 不会调用 CurrencyExchangeActivity
。我知道它出现是因为方法 onCreate()
只调用了一次,但我不能把它放在 onResume()
中,因为当我再次打开我的应用程序时,它会再次显示启动画面。但我想显示 CurrencyActivity
,例如 google 的应用程序(地图、表格等...)
请试试这个方法。我没有测试过你的代码。我使用 splash 的方式如下:
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();
}
}
您忘记添加了
setContentView(R.layout.splash);
添加这个就可以了
也许您可以尝试以下方法:
@Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
这将跳过按主页按钮后重新打开应用程序时的延迟。
我通常使用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);
}
}
您不能仅仅因为将初始屏幕图像设置为 Activity
的 theme
的 background
就可以这样做。那是 window 级别的背景,无论您的导航或任何其他内容都会显示。
如果您真的想在应用程序生命周期中只显示一次启动画面,那么您必须在 not the right way
中执行此操作并将该图像作为布局的一部分并使用 [=14= 扩展该布局].现在,当您再次进入该应用程序时,您甚至会在 splash activity
的 setContentView
之前调用您的 CurrencyExchangeActivity
并且只会显示黑色 window 背景并直接显示 CurrencyExchangeActivity
.
让我知道这是否有意义。如果需要我可以详细说明
我建议在您的案例中保留 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);
}
}
试试这个--
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>
我测试了这段代码,如果你遇到按下主屏幕按钮时启动画面再次显示的问题,那么这就可以了....