Here is my activity Mod_1_1_b. on startup, this activity takes user to Mod_1_1_c after 3 sec,
Mod_1_1_b has a button for going to some other activity (main) but whenever i press that button, it first takes me to main activity and then immediately launches Mod_1_1_c. i have tried calling finish(); but not working, looks like the intent already launches in background.
package com.example.abc;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
public class Mod_1_1_b extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mod_1_1_b);
final MediaPlayer mp1 = MediaPlayer
.create(getBaseContext(), R.raw.yolo); // -<
mp1.start();
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent startActivity = new Intent(Mod_1_1_b.this,
Mod_1_1_c.class);// -<
startActivity(startActivity);
overridePendingTransition(R.anim.slide_right_to_left_1,
R.anim.slide_right_to_left_2);
finish();
}
}, 3000L);
}
public void back(View view) {
Intent intent = new Intent(this, Mod_1_1AtoZ.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_left_to_right_1,
R.anim.slide_left_to_right_2);
finish();
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
}
}
xml
<RelativeLayout
android:layout_width="290dip"
android:layout_height="290dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/b_tile" >
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="back"
android:text="Back" />
</RelativeLayout>
it first takes me to main activity and then immediately launches Mod_1_1_c
That is because you set your handler to execute in 3 seconds thus after you press that button it will change the activity but it will still execute your handler and change to activity Mod_1_1_c
solution:
In you button back you need to cancel your handler to stop it executing after you changed activity.
sample:
public void back(View view) {
timeoutHandler.removeCallbacks(your_runnable);
Intent intent = new Intent(this, Mod_1_1AtoZ.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_left_to_right_1,
R.anim.slide_left_to_right_2);
finish();
}
Make sure that your Runnable and Handler has global instance of it.
EDIT:
public class Mod_1_1_b extends Activity {
Handler mHandler; //global instance
Runnable your_runnable; //global instance
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mod_1_1_b);
final MediaPlayer mp1 = MediaPlayer
.create(getBaseContext(), R.raw.yolo); // -<
mp1.start();
mHandler = new Handler();
your_runnable = new Runnable() {
#Override
public void run() {
Intent startActivity = new Intent(Mod_1_1_b.this,
Mod_1_1_c.class);// -<
startActivity(startActivity);
overridePendingTransition(R.anim.slide_right_to_left_1,
R.anim.slide_right_to_left_2);
finish();
}
};
mHandler.postDelayed(your_runnable , 3000L);
}
public void back(View view) {
mHandler.removeCallbacks(your_runnable);
Intent intent = new Intent(this, Mod_1_1AtoZ.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_left_to_right_1,
R.anim.slide_left_to_right_2);
finish();
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
}
}
Related
I have my first activity that switch to the second one after 3 seconds and this works fine. The problem is that if i press the Home Button during this 3 seconds, the app reopen in the second activity. Is there a simple way to fix this?
Thanks in advance.
public class StartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
switchActivities();
}
#Override
public void onBackPressed(){
}
public void switchActivities() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
finish();
}
}, 3000);
}
}
EDIT:
Maybe I wasn't clear, I do not want the app to reopen once I press the home button. How can I do this?
This is because you don't clear your delayed callback. You can fix it in this way:
private Handler handler = new Handler();
public void switchActivities() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
finish();
}
}, 3000);
}
public void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
You just have to cancel your Handler when you leave your first activity before the second one opens.
public class StartActivity extends AppCompatActivity {
Runnable nextActivityRunnable;
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
switchActivities();
}
#Override
public void onBackPressed(){
}
public void switchActivities() {
nextActivityRunnable = new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(intent);
finish();
}
};
handler = new Handler();
handler.postDelayed(nextActivityRunnable, 3000);
}
#Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(nextActivityRunnable);
}
}
I want to stop the delay/intent from processing when the back button is pressed. I've searched some threads but I don't have the same Handler logic, I think. I apologize if it is.
Here is the code I'm working with:
else {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, PollWebView_Gilmore.class);
startActivity(intent);
finish();
}
}, 10000);
You have to save a reference to both the Handler and your Runnable somewhere, and then you can use the removeCallbacks(Runnable) method on the Handler to cancel the pending request.
Example code:
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handler.postDelayed(runnable, 5000);
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
handler.removeCallbacks(runnable);
}
}
If you Don't need a delay why do you use a Handler? Just remove it!
Call this directly
Intent intent = new Intent(MainActivity.this, PollWebView_Gilmore.class);
startActivity(intent);
finish();
When your back button is pressed!
You can also get an idea by reading below posts.
Android: Proper Way to use onBackPressed() with Toast
How to run a Runnable thread in Android?
If you want to do something with the intent on back pressed.You can override onBackPressed() method.
currently I'm programming an Android application and I'm using timer for my splash. instead of using timer I want to use handle but I can't integrate that to my code:
package com.tesbih;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
}catch(InterruptedException e){
e .printStackTrace();
}finally{
Intent openStartingPoint = new Intent ("com.tesbih.TESBIHMAINACTIVITY");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
}
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Intent openStartingPoint = new Intent(this, TESBIHMAINACTIVITY);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
startActivity(openStartingPoint);
finish();
}
}, 5000);
}
}
I want to write a code that uses the splash screen .I have written this so far, but Can anyone tell me what is the missing here!?
here is my main code:
package com.example.splash;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
and here is my splash activity code:
package com.example.splash;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class splashscreen extends Activity {
protected int _splashTime = 5000;
private Thread splashTread;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splashh);
final splashscreen sPlashScreen = this;
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized(this){
wait(_splashTime);
}
} catch(InterruptedException e) {}
finally {
finish();
Intent i = new Intent();
i.setClass(sPlashScreen,MainActivity.class);
startActivity(i);
//stop();
}
}
};
splashTread.start();
}
The problem is I do not know how to tell my main to go splash activity , if I use an intent I would stuck on infinite loop.
You can simply use this:
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
Intent intent = new Intent(SplashViewController.this,HomeViewController.class);
startActivity(intent);
SplashViewController.this.finish();
}
}, 3000);
try this instead :
public class splashscreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
Thread t = new Thread(Splash_Runnable);
t.start();
}
Runnable Splash_Runnable = new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000);
startActivity(new Intent(splashscreen.this,
MainActivity.class));
splashscreen.this.finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
The problem (i guess) is that your app is starting with your MainActivity as your launcher Activity. Make splashscreen your laucher Activity in your Application Manifest XML and you will avoid the infinite loop.
Try this code:
private boolean _active = true;
private int _splashTime = 5000;
Thread splashTread = new Thread()
{
#Override
public void run()
{
try
{
int waited = 0;
while(_active && (waited < _splashTime))
{
sleep(100);
if(_active)
{
waited += 100;
}
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(SplashScreenActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
};
splashTread.start();
in AndroidManifest mention your activity as Main Activity.
Try to change your SplashActivity code from here.
Splash and main activity error
Also make your splashactivtiy as your launcher activity and then redirect to the MainActivity from the SplashScreen
<activity
android:name="com.app.wablogic.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Full detail of creating a splash Activity
Create a layout for Splash
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/splash" >
</LinearLayout>
Now create a class Under package . Name it Splash
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();
}
}, 5000); //it will call the MainActivity after 5 seconds
}
Go to manifest and add the Activity to it.
and cut the intent-filter where main and Launcher are child and paste it in Splash Activity like
<activity
android:name="com.example.yourpackage.Splash"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can create a Thread for doing something or just sleep for a few seconds to do, such as
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 3 seconds
sleep(3*1000);
// After 3 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),MenuActivity.class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
background.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
You can get more example here.
I am making an application in which I have to show a welcome page for only 5 seconds and then redirect to another activity. However, I am not getting what I want because I am getting another activity first and MainActivity after click on the back button of the emulator which is not right. So could somebody please help me?
I am placing some code for your reference specially welcome activity and manifest code:
public class Welcome extends Activity
{
private Runnable runnable;
private static final String TAG="Welcome";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
runnable=new Runnable() {
public void run() {
try
{
Log.v(TAG,"Going to sleep...");
Thread.sleep(3000);
Log.v(TAG,"Going to wake up...");
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
try
{
Thread t=new Thread(null,runnable);
t.start();
Intent i=new Intent(this,TabSample.class);
startActivity(i);
}
catch(Exception e)
{
}
}
}
AndroidManifest.xml
<activity
android:name=".Welcome"
android:label="Restaurant Application" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TabSample"></
main.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:src="#drawable/home" />
use intent like this
Intent i=new Intent(Welcome.this,TabSample.class);
startActivity(i);
You should try this.
public class Welcome extends Activity
{
private Runnable runnable;
private static final String TAG="Welcome";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
runnable=new Runnable() {
public void run() {
try
{
Log.v(TAG,"Going to sleep...");
Thread.sleep(3000);
Log.v(TAG,"Going to wake up...");
runOnUiThread(new Runnable() .
{
#Override
public void run()
{
Intent i=new Intent(this,TabSample.class);
startActivity(i);
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
try
{
Thread t=new Thread(null,runnable);
t.start();
}
catch(Exception e)
{
}
}
}
Because if you start Activity with parallel to threat then it will go forward without any delay and thread will run in background. so put you startactivity code in runonuithread. like above.
it may work for you.
Try this
Thread thread = new Thread() {
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(Welcome.this, TabSample.class);
startActivity(intent);
finish();
}
};
thread.start();
you can use timertask for showing the Welcome screen for some time
So here is how you go about it.
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
public class Welcome extends Activity {
private long splashDelay = 5000; //5 seconds
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TimerTask task = new TimerTask()
{
#Override
public void run() {
finish();
Intent mainIntent = new Intent().setClass(Welcome.this, TabSample.class);
startActivity(mainIntent);
}
};
Timer timer = new Timer();
timer.schedule(task, splashDelay);
}
}
if you only need to show the Welcome Activity for 5 seconds modify your thread. Remove and insert
Intent i=new Intent(this,TabSample.class);
startActivity(i);
inside runnable.
You Can only use a handler to do that . code will be like this
Runnable GotoMenu;
Handler CallMenu = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
GotoMenu = new Runnable(){
public void run(){
CallMenu.removeCallbacks(GotoMenu);
Intent i = new Intent(getApplicationContext(), TabSample.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
};
CallMenu.postDelayed(GotoMenu, 2000);
}