Reputation: 355
what i'm trying to do is a home screen that stays for 5 seconds and goes to activity1.When i click a button in activity1 leads me to activity2.I've tried many times to click the button but no switching happens. homescreen (5 seconds)=Main_Activity Activity1=selectpets.java Activity2=fishtank.java
onclick listener seems the problem i don't know what's 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 the 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 just no switching after clicking
thank you very much
Upvotes: 6
Views: 17419
Reputation:
Avoid doing 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 gives you the Intent mechanism for switching Activities
i.e. use
startActivity(new Intent(this, yourSecondActivity.class));
instead of
setContentView(R.layout.selectscreen);
The remaining part of your code must work fine.
Upvotes: 1
Reputation: 77904
You can use finish()
if you want 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);
Be sure that you have 2nd Activity defined in your Manifest.xml:
<activity android:name="x.x.SelectPetsScreen"
android:theme="@style/NoTitle"
android:screenOrientation="nosensor"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Upvotes: 1
Reputation: 355
I was able to solve it with the help of Maxim Shoustin and everyone by: adding second Activity to Manifest.xml which is SelectPetsScreen Thank you so much
Upvotes: 0
Reputation: 44571
Here you aren't ever switching to the next Activity
, just changing the layout
of the 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 an Intent
Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
startActivity(i);
Since you aren't actually going to the next Activity
(java file) your onClick()
isn't set.
Edit
This is what you are doing
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 be doing. Notice 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);
}
Upvotes: 4