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
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);
}
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>
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.
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
Related
I am working on an app and while testing on my phone a noticed something weird: if I close the app while the splash screen is running, when the time of the before mention activity is over, the app opens again, even if I am in another app. Why does this happen?
package com.example.arlet.storemaps;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import java.util.Timer;
import java.util.TimerTask;
public class SplashScreenActivity extends AppCompatActivity {
//duration of splash screen in miliseconds
long delay = 6000;
private Timer timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
Timer RunSplash = new Timer();
TimerTask ShowSplash = new TimerTask() {
#Override
public void run() {
//finishing splash screen
finish();
//starting main activity
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
}
};
RunSplash.schedule(ShowSplash, delay);
}
#Override
protected void onDestroy(){
timer.cancel();
timer.purge();
super.onDestroy();
}
}
Here's the code updated #Badran
Override the onDestroy Method in SplashActivity:
#Override
protected void onDestroy() {
//remove the handler or thread or etc... that is opening the another activity
//call timer.cancel()
//call timer.purge ()
super.onDestroy();
}
So Your code will be now :
package com.example.arlet.storemaps;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import java.util.Timer;
import java.util.TimerTask;
public class SplashScreenActivity extends AppCompatActivity {
//duration of splash screen in miliseconds
long delay = 6000;
private Timer RunSplash;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
RunSplash = new Timer();
TimerTask ShowSplash = new TimerTask() {
#Override
public void run() {
//finishing splash screen
finish();
//starting main activity
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
}
};
RunSplash.schedule(ShowSplash, delay);
}
#Override
protected void onDestroy(){
if(RunSplash != null){
RunSplash.cancel();
RunSplash.purge();
}
super.onDestroy();
}
#Override
protected void onPause() {
if(RunSplash != null){
RunSplash.cancel();
RunSplash.purge();
}
super.onPause();
}
}
I'm really stuck here. I'm trying to navigate to a second activity using a button but whenever i try to parse in the name of the class to the Intent method, Android Studio throws an error.
In the Intent method toWeightsScreen, it won't let me parse in a class.
Can anyone tell me what I'm messing up please.android studio snapshot
package leith.comstephen.facebook.httpswww.fitnessapp5;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button navToWeightsScreen = (Button)findViewById(R.id.Firstweights);
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(this, cut.class)
startActivity(toWeightsScreen);
}
});
}
}
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(getActivity(), cut.class);
startActivity(toWeightsScreen);
}
});
The this in your intent declaration refers to the button's setOnClickListener.
You should specify the actual activity. See the code below
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Changed cut.class to Cut.class to follow coding conventions
Intent toWeightsScreen = new Intent(MainActivity.this, Cut.class);
startActivity(toWeightsScreen);
}
});
Previously I had error - something about some loop, I've seen info that it is necessary in that case to start a new thread for button, but still nothing happens, thought logs show no errors now.
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int CAPTURE = 9003;
Button button;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.capture);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, "BUTTON clicked", Toast.LENGTH_LONG).show();
Log.d("BUTTON","CLICKED");
}
});
}
});
Intent intent = new Intent(this, CaptureActivity.class);
intent.putExtra(CaptureActivity.AutoFocus, true);
startActivityForResult(intent, CAPTURE);
}
}
runOnUiThread mean you are telling the UI thread to execute the passed Runnable as instruction so it won't create a new thread.
Solution : I assume you this code is for demo purpose for threading and UI updation so one of best alternative is to AsyncTask
and remove runOnUiThread function , no need of it
You need to remove this code or move it inside run method
Intent intent = new Intent(this, CaptureActivity.class);
intent.putExtra(CaptureActivity.AutoFocus, true);
startActivityForResult(intent, CAPTURE);
Because you are currently in CaptureActivity not on MainActivity.
OnCreate will directly take you to the CaptureActivity where you are expecting the code of MainActivity to run (probably they have same UI)
I have a simple form in the main activity. On submitting the form, activity 2 starts. When I go back to the main activity (by clicking on the back button), I want to clear all the responses.
When you assign an id to any widget, and the activity or fragment stop, then the onSaveState is called for any widget to save the actual value and show it when you come back to that activity.
So, to clean up your EditText when you came back from another activity you'll have to do it manually, I suggest you to do it in onRestart function according to the Activity life cycle
You could overwrite onResume() in your main activity and clear the corresponding EditTexts there like
#Override
public void onResume() {
editText.setText("");
}
or
You can overwrite onBackPressed() in activity 2 like this
#Override
public void onBackPressed() {
Intent intent = new Intent(Activity2.this, MainActivity.class);
startActivity(intent);
}
I don't know an elegant solution, but it should works:
public class MainActivity extends AppCompatActivity {
private boolean secondActivityLaunched;
#Override
protected void onCreate(Bundle savedInstanceState) {
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
secondActivityLaunched = true;
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
#Override
protected void onStart() {
super.onStart();
if (secondActivityLaunched) {
secondActivityLaunched = false;
clearForm();
}
}
}
1st Activity: MainActivity.java
package com.test.activitytest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText name=(EditText) findViewById(R.id.name);
EditText email=(EditText) findViewById(R.id.email);
EditText phone=(EditText) findViewById(R.id.phone);
Button submit = (Button)findViewById(R.id.button);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent it=new Intent(MainActivity.this,Activity2.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
finish();
}
});
}
}
2nd Activity: Activity2.java
package com.test.activitytest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent it=new Intent(Activity2.this,MainActivity.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
finish();
}
}
Hello StackOverflow Users,
I am new to android and trying to develop a game in which I use a
1) Main class to redirect (like a menu.. new game, options, help, exit etc..)
2) A surfaceview class
3) A thread to handle drawing on canvas.
I have added an exit button on the main class.
However after playing the game i.e. drawing the objects and using them, when i redirect to my Main class and try to exit; the main screen disappears but the view and threads aren't destroyed.
This is the main class.
package com.tgm.welcome;
import com.tgm.R;
import com.tgm.main.GThread;
import com.tgm.main.TGMActivity;
import com.tgm.options.OptionsMain;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class Welcome_Act extends Activity {
ImageView game, exit, options;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
game = (ImageView) findViewById(R.id.newGame);
options = (ImageView) findViewById(R.id.options);
exit = (ImageView) findViewById(R.id.exit);
game.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
gotogame();
}
});
options.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
goto_opt();
}
});
exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
exit_game();
}
});
}
public void gotogame() {
Intent game = new Intent(Welcome_Act.this, TGMActivity.class);
startActivity(game);
}
public void goto_opt() {
Intent opt = new Intent(Welcome_Act.this, OptionsMain.class);
startActivity(opt);
}
public void exit_game() {
System.exit(0);
}
}
PLEASE HELP TO REMOVE THE GAMESCREEN FROM THE STACK THAT ANDROID MAINTAINS.
Thanks..
Using System.exit(0) is not advised in android. It doesnt guarantee finishing the Activity.
Instead of
public void exit_game() {
System.exit(0);
}
Use:
public void exit_game() {
Welcome_Act.finish();
}
Just call finish on the activity you want to remove from the stack.. it does the work you want..