初始屏幕活动



我向应用程序添加了初始屏幕,我的代码如下所示:

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,比如谷歌的应用程序(地图、表格等......

不能仅仅因为您将初始屏幕图像设置为theme Activitybackground而这样做。这是窗口级别的背景,无论您的导航或任何内容都会显示。

如果您确实想在应用生命周期中仅显示一次初始屏幕,则必须在not the right way中执行此操作,并将该图像作为布局的一部分,并使用setContentView膨胀该布局。现在,当您再次进入应用程序时,您甚至可以在splash activity setContentView之前致电您的CurrencyExchangeActivity,这只会显示黑色窗口背景并直接显示CurrencyExchangeActivity

让我知道这是否有意义。如果需要,我可以详细说明<</p>

div class="one_answers">请尝试

这种方式。我还没有测试过你的代码。我使用飞溅的摇摆将如下所示:

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

这将跳过按下主页按钮后重新打开应用程序时的延迟。

我通常使用postDelay 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);
    }
}

我想建议在您的情况下将初始屏幕显示机制保留在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);
    }
}

试试这个——

启动画面活动-

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>

我测试了这段代码,如果你遇到问题,当按下主屏幕按钮时,启动画面再次显示,那么这将起作用......

最新更新