Android Switch between actions

what I'm trying to do is a home screen that stays on for 5 seconds and goes into activity. When I press a button in action1, I behave in action. I tried many times to press a button, but switching does not happen, home screen (5 seconds) = Main_Activity Activity1 = selectpets.java Activities2 = fishtank.java

The onclick listener seems to be a problem, I donโ€™t know what is wrong with it.

Main Activity Code package com.set.petshome; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { Button fButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Delay Code after 5 seconds new Handler().postDelayed(new Runnable() { @Override public void run() { setContentView(R.layout.selectscreen); //where <next> is you target activity :) } }, 5000); } //Delay End @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

Now Selectpets Code

 package com.set.petshome; import android.app.Activity; import android.content.*; import android.os.Bundle; import android.view.*; import android.widget.Button; public class SelectPetsScreen extends Activity { Button fButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.selectscreen); //Button Fishtank Listener Start fButton = (Button) findViewById(R.id.button1); //Listening to button event fButton.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { //Starting a new Intent Intent nextScreen = new Intent(getApplicationContext(), fishtank.class); startActivity(nextScreen); } }); //Button Fishtank Listener End } } 

Fishtank Class Code

 package com.set.petshome; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; public class fishtank extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ftank); } } 

By the way, no errors in the application simply switch after clicking the button, thank you very much

+4
source share
4 answers

Here you never go to the next Activity , just changing the layout current Activity

  new Handler().postDelayed(new Runnable() { @Override public void run() { setContentView(R.layout.selectscreen); //where <next> is you target activity :) } }, 5000); 

instead of setContentView() you need to use Intent

 Intent i = new Intent(MainActivity.this, SelectPetsScreen.this); startActivity(i); 

Since you are not really going to the next Activity (java file), your onClick() not installed.

Edit

This is what you do

 public class MainActivity extends Activity { Button fButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Delay Code after 5 seconds new Handler().postDelayed(new Runnable() { @Override public void run() { setContentView(R.layout.selectscreen); //where <next> is you target activity :) } }, 5000); } 

This is what you should do. Note the difference in the run() function

 public class MainActivity extends Activity { Button fButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Delay Code after 5 seconds new Handler().postDelayed(new Runnable() { @Override public void run() { Intent i = new Intent(MainActivity.this, SelectPetsScreen.this); startActivity(i); } }, 5000); } 
+4
source

Avoid this.

  setContentView(R.layout.activity_main); //Delay Code after 5 seconds new Handler().postDelayed(new Runnable() { @Override public void run() { setContentView(R.layout.selectscreen); //where <next> is you target activity :) } }, 5000); 

Google provides you with an Intent mechanism for switching actions

i.e. use

 startActivity(new Intent(this, yourSecondActivity.class)); 

instead

 setContentView(R.layout.selectscreen); 

The rest of your code should work fine.

+1
source

You can use finish() if you want to use firts Activity only once.

 mHandler.postDelayed(new Runnable() { @Override public void run() { startActivity(new Intent().setClass(MainActivity.this, SelectPetsScreen .class).setData(getIntent().getData())); finish(); } }, 5000); 

Make sure you have the 2nd operation defined in your .xml manifest:

 <activity android:name="xxSelectPetsScreen" android:theme="@style/NoTitle" android:screenOrientation="nosensor" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> </intent-filter> </activity> 
+1
source

I was able to solve this with the help of Maxim Shustin and everyone: adding a second operation to Manifest.xml, which is SelectPetsScreen Thank you so much.

0
source

Source: https://habr.com/ru/post/1497230/


All Articles