简体   繁体   English

仅在应用程序启动时显示启动画面,而不是每次调用包含启动画面代码的活动时显示启动画面

[英]Display splash screen only when the app is launched and not every time when the activity containing splash screen code, is called

Before I get into describing by problem I'd like to point out I am aware of the other threads asking this question, however none for me have been able to solve my issue.在我开始按问题进行描述之前,我想指出我知道其他线程在问这个问题,但是对我来说没有一个能够解决我的问题。

The difference between previously asked questions and my question is that I've splash screen code in the main activity and NOT in another activity, so i have no layout XML file for this.先前提出的问题与我的问题之间的区别在于,我在主活动中而不是在另一个活动中使用了启动画面代码,因此我没有为此设置的布局 XML 文件。

So, the problem is, I want to display splash screen only when the app is started, and this is done perfectly by the following code but when the main activity (which contains splash screen code) is called by another activity using Intent , the splash screen is displayed again.所以,问题是,我只想在应用程序启动时显示闪屏,这可以通过以下代码完美地完成,但是当main activity (包含闪屏代码)被另一个使用Intent活动调用时,闪屏屏幕再次显示。

So, first I tried to use flag variable, initiating 0 when activity is created and incrementing it by one when splash screen is displayed once.因此,首先我尝试使用flag变量,在创建活动时初始化0 ,并在显示一次闪屏时将其递增 1。 but I figured out that it will not work because it will always 0 when activity is created.但我发现它不起作用,因为在创建活动时它始终为0

So, now i'm trying to pass a string from the other activity and trying to prevent splash screen again as shown in following code:所以,现在我试图从另一个活动传递一个字符串,并试图再次防止闪屏,如下面的代码所示:

public class Registration extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.kaushal.myapplication.MESSAGE";

DatabaseHelper myDb;
EditText username,password;
private ImageView splashImageView;
boolean splashloading = false;
int flag=0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registration);


    String message = null;
    Intent intent0 = getIntent();
    message = intent0.getStringExtra(Registration.EXTRA_MESSAGE);//string from another activity.

    if(!message.equals("signup has called me")) { //splash screen code in `if`
        splashImageView = new ImageView(this);
        //splashImageView.setBackgroundColor();
        splashImageView.setImageResource(R.drawable.sexy);
        setContentView(splashImageView);
        splashloading = false;
        Handler h = new Handler();

        h.postDelayed(new Runnable() {
            public void run() {
                splashloading = false;
                setContentView(R.layout.activity_registration);
                username = (EditText) findViewById(R.id.username);
                password = (EditText) findViewById(R.id.password);
            }
        }, 3000);
        flag++;
    }

    if(flag==0){
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
    }

    myDb = new DatabaseHelper(this);




}
}

Here Registration is name of my main activity which is launched when the app will be started.这里Registration是我的主要活动的名称,它在应用程序启动时启动。

Code of method of another activity named sign Up which passes a string:另一个名为sign Up活动的方法代码,它传递一个字符串:

 public void backToRegistration(View view){
    Intent intent0 = new Intent(this,Registration.class);
    intent0.putExtra(EXTRA_MESSAGE, "signup has called me");
    startActivity(intent0);
}

What is the problem with this method?这种方法有什么问题? App is crashes immediately after launching.应用程序启动后立即崩溃。

So there are a number of issues with using a String in an Intent as a flag like this.因此,在 Intent 中使用 String 作为标志存在许多问题。 Strings aren't really setup for it.字符串并没有真正为它设置。 The overhead is a good bit more than that of a simple primitive boolean flag.开销比简单的原始布尔标志要多得多。

To do this the way you are currently trying以您目前正在尝试的方式执行此操作

Change your onCreate code to this将您的onCreate代码更改为此

super.onCreate(savedInstanceState);

Intent intent0 = getIntent();
boolean showSplash = getIntent().getBooleanExtra(Registration.FLAG_KEY), true);

handleSplashScreen(showSplash);

And add this method, which we will use in a somewhat recursive way to handle showing the splash screen.并添加此方法,我们将以某种递归方式使用它来处理启动画面的显示。

private void handleSplashScreen(boolean showSplash){
    if(showSplash) { //splash screen code in `if`
        splashImageView = new ImageView(this);
        //splashImageView.setBackgroundColor();
        splashImageView.setImageResource(R.drawable.sexy);
        setContentView(splashImageView);
        splashloading = false;
        Handler h = new Handler();

        h.postDelayed(new Runnable() {
           public void run() {
               handleSplashScreen(false);
           }
        }, 3000);
    }
    else{
        splashloading = false;
        setContentView(R.layout.activity_registration);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
    }
}

Then, when you are opening this activity from another activity do然后,当您从另一个活动打开此活动时,请执行

public void backToRegistration(View view){
    Intent intent0 = new Intent(this,Registration.class);
    intent0.putExtra(FLAG_KEY, false);
    startActivity(intent0);
}

Where FLAG_KEY is private static FLAG_KEY = com.kaushal.myapplication.FLAG;其中FLAG_KEYprivate static FLAG_KEY = com.kaushal.myapplication.FLAG; and replaces the EXTRA_MESSAGE string.并替换EXTRA_MESSAGE字符串。

How I would recommend doing it我建议如何做

Now, in saying that, this isn't at all how I would recommend doing this.现在,这么说,这根本不是我建议这样做的方式。 You should simply have a SplashScreenActivity that is called on launch and redirects to your Registration MainActivity.您应该简单地拥有一个在启动时调用并重定向到您的Registration MainActivity 的 SplashScreenActivity。 This get's rid of the convolution of having an Activity with two different layouts, flags, etc.这摆脱了具有两种不同布局、标志等的 Activity 的卷积。

public class SplashScreenActivity extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run(){
                startActivity(new Intent(SplashScreenActivity.this, MainActivity.class);
                SplashScreenActivity.this.finish();
            }
        }, 3000);

    }
}

Where R.layout.activity_splash looks like this R.layout.activity_splash 看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="R.drawable.sexy"
    android:scaleType="centerInside" />

Then you could have MainActivity like this那么你可以像这样拥有 MainActivity

public class MainActivity extends ActionBarActivity{

    // Variables here
    ...

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);

        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
    }

    // Other methods
    ...
}

Lastly, simply go in the manifest and update it so that the newly created SplashScreenActivity is shown on launch and make sure that the MainActivity is still declared appropriately.最后,只需进入清单并更新它,以便在启动时显示新创建的 SplashScreenActivity 并确保 MainActivity 仍然正确声明。

<activity
    android:name=".SplashScreenActivity"
    android:label="@string/app_name">

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

<activity
    android:name=".MainActivity"/>

No more flags, no more logic, no more issues.没有更多的标志,没有更多的逻辑,没有更多的问题。 It loads the splash screen on launch and that is it.它在启动时加载启动画面,就是这样。 The Activity finishes after it passes the intent so it is no longer on the stack.活动在传递意图后完成,因此它不再位于堆栈中。 All is well.一切都很好。 The reason that the other questions handle things like this is it is the recommended way of going about having a splash screen.其他问题处理这样的事情的原因是它是进行启动画面的推荐方式。

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

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