晚上好,我将首次在智能手机上启动应用程序启动一个活动主要布局,我该怎么办? 这是我的主要活动...当我的apo开始时你可以看到我的activity_main选择通知来自firebase,当我设置通知时,当我放入webview时,app会将你重定向到另一个activity_mainweb,我只想看到第一次活动_主要布局
公共类MainActivity扩展了AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// If a notification message is tapped, any data accompanying the notification
// message is available in the intent extras. In this sample the launcher
// intent is fired when the notification is tapped, so any accompanying data would
// be handled here. If you want a different intent fired, set the click_action
// field of the notification message to the desired intent. The launcher intent
// is used when no click_action is specified.
//
// Handle possible data accompanying notification message.
// [START handle_data_extras]
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
// [END handle_data_extras]
Button subscribeButton = (Button) findViewById(R.id.subscribeButton0);
subscribeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// [START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("INTERIORDESIGN150H");
// [END subscribe_topics]
// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Button subscribeButton1 = (Button) findViewById(R.id.subscribeButton1);
subscribeButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// [START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("INTERIORDESIGN300H");
// [END subscribe_topics]
// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Button subscribeButton2= (Button) findViewById(R.id.subscribeButton2);
subscribeButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// [START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("GARDENDESIGN150H");
// [END subscribe_topics]
// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Button subscribeButton3 = (Button) findViewById(R.id.subscribeButton3);
subscribeButton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// [START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("GARDENDESIGN300H");
// [END subscribe_topics]
// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Button subscribeButton4 = (Button) findViewById(R.id.subscribeButton4);
subscribeButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// [START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("VISUALDESIGN150H");
// [END subscribe_topics]
// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Button subscribeButton5 = (Button) findViewById(R.id.subscribeButton5);
subscribeButton5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// [START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("PRODUCTDESIGN150H");
// [END subscribe_topics]
// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Button subscribeButton6 = (Button) findViewById(R.id.subscribeButton6);
subscribeButton6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// [START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("MOTIONDESIGN150H");
// [END subscribe_topics]
// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Button subscribeButton7 = (Button) findViewById(R.id.subscribeButton7);
subscribeButton7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// [START subscribe_topics]
FirebaseMessaging.getInstance().subscribeToTopic("GRAPHICDESIGN150H");
// [END subscribe_topics]
// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Button Button = (Button) findViewById(R.id.subscribeButton0);
Button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(MainActivity.this, MainActivityweb.class));
}
});
Button Button1 = (Button) findViewById(R.id.subscribeButton1);
Button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(MainActivity.this, MainActivityweb.class));
}
});
Button Button2 = (Button) findViewById(R.id.subscribeButton2);
Button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(MainActivity.this, MainActivityweb.class));
}
});
Button Button3 = (Button) findViewById(R.id.subscribeButton3);
Button3.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(MainActivity.this, MainActivityweb.class));
}
});
Button Button4 = (Button) findViewById(R.id.subscribeButton4);
Button4.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(MainActivity.this, MainActivityweb.class));
}
});
Button Button5 = (Button) findViewById(R.id.subscribeButton5);
Button5.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(MainActivity.this, MainActivityweb.class));
}
});
Button Button6 = (Button) findViewById(R.id.subscribeButton6);
Button6.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(MainActivity.this, MainActivityweb.class));
}
});
Button Button7 = (Button) findViewById(R.id.subscribeButton7);
Button7.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(MainActivity.this, MainActivityweb.class));
}
});
}
}
答案 0 :(得分:1)
您需要使用SharedPrefs来跟踪它是否是第一次启动并重定向到另一个布局(如果是
)private static final String KEY_HAS_BEEN_LAUNCHED_BEFORE = "KEY_HAS_BEEN_LAUNCHED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean hasBeenLaunchedBefore = prefs.getBoolean(KEY_HAS_BEEN_LAUNCHED_BEFORE , false);
setContentView(hasBeenLaunchedBefore ? R.layout.normal_layout
: R.layout.first_launch_layout);
if (!hasBeenLaunchedBefore) {
//after the first launch, record that it's been seen
final SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(KEY_HAS_BEEN_LAUNCHED_BEFORE , true);
editor.apply();
}
}