I am new in Android Development. I have been trying to figure out how to display a screen in android studio only for 5 seconds and then get transfered into a new activity.
For example:
Activity A -> Activity B (Shown for 5 seconds) -> Activity C
Also I want to make sure that when a user clicks on the back button while he is in Activity B nothing happens (It doesnt go back to Activity A).
What is the easiest way to do that?
I know I have to use Intent.
try this. I have commented it out, but if you have any questions about it feel free to ask.
public class ClassB extends AppCompatActivity {
//Handler allows you to send and process Runnable Objects (Classes in this case)
private Handler mHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_classb);
//postDelayed method, Causes the Runnable r (in this case Class B) to be added to the message queue, to be run
// after the specified amount of time elapses.
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
//Create a new Intent to go from Class B to Class C and start the new Activity.
Intent intent = new Intent(ClassB.this, ClassC.class);
startActivity(intent);
finish()
}
//Here after the comma you specify the amount of time you want the screen to be delayed. 5000 is for 5 seconds.
}, 5000);
}
//Override onBackPressed method and give it no functionality. This way when the user clicks the back button he will not go back.
public void onBackPressed() {
} }
In Kotlin you can do:
Handler().postDelayed({
// Start activity
startActivity(Intent(this, YourTargetActivity::class.java))
// terminate this activity(optional)
finish()
}, 5000)
Related
I have the following activity path:
Main -> A0 -> A1 -> A2 -> B
or
Main -> B
On Main, the user can select to either show something on B, or to create something new with the A series.
After completing the A series, it goes to B.
When the user gets to B via the A route, I want the back button to go to Main. But when the user is in the A series, I want the back button to go to the previous A (or Main from the first A).
I tried using FLAG_ACTIVITY_NO_HISTORY when I create the intents, but that just makes everything go back to Main.
Is there a way to mark the activities for removal once I hit a threshold?
You could add a BroadcastReceiver in all activities you want to close (A0, A1, A2):
public class MyActivity extends Activity {
private FinishReceiver finishReceiver;
private static final String ACTION_FINISH =
"com.mypackage.MyActivity.ACTION_FINISH";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finishReceiver= new FinishReceiver();
registerReceiver(finishReceiver, new IntentFilter(ACTION_FINISH));
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(finishReceiver);
}
private final class FinishReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_FINISH))
finish();
}
}
}
To close these activities trigger the following broadcast from Activity B.
sendBroadcast(new Intent(ACTION_FINISH));
Here is the github example project for the same.
Define the logic in this very way
In Series of Activity A define the logic in this very way
public static Activity A=null;
//in onCreate();
A=this;
//Same way in B
public static Activity B=null;
B=this
now wherever you want the back condition to be checked override the OnbackPressed.
#Override
public void onBackPressed()
{
if(Activity_A.A!=null)
{
finish();
return;
}
if(Activity_B.B!=null){
//your condition
finish();
}
}
You can fire a new intent for Main activity with SINGLE_TOP and CLEAR_TOP flags in onBackPressed of B. So no matter how you landed at B, pressing back takes you to Main with all history cleared. No need to mess with A series activities.
I have an app with a title screen. When the app first starts, I have an onCreate method that contains the following code:
setContentView(R.layout.title_screen);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.main_screen);
}
}, 2000);
When I run my app and press the back button while on the main_screen layout, it closes the app (as it should). However, when I reopen the app, it displays the title_screen layout for two seconds again even though the app is already running. How can I prevent this?
This will prevent the delay appearing again when resumed:
private static boolean flag = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!flag){
setContentView(R.layout.title_screen);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.main_screen);
}
}, 2000);
flag = true;
} else {
setContentView(R.layout.main_screen);
}
}
Btw, if your app was on background and it is calling onCreate again while being resumed, it means that it is killed by the OS. Therefore it is normal to have the initial delay again.
What I would do is to implement two different activities first one showing title_screen and the second which is started after 2s should show your main screen.
After looking at your code I can see that you ALWAYS start with title_screen then after 2s, you change to main_screen. Therefore, when you press back, that means you finish your activity. When you re-open your app, onCreated is called again, and it run every line of code as the previous opening.Of course, there's no difference in 2 times you open your app. To overcome it, I recommend to use SharedPreference to store the flag to check main_screen or title_screen.
Ok I'm working on a splash screen that pauses for 1.5 seconds and works great, except for one thing. Once the timer is started in onCreate if the configuration (orientation) changes then the timer gets reset and then end result is it starts my ParchmentActivity.java twice.
How can I prevent the handler from sending the intent twice?
Thanks in advance!
Full code can be found #: https://github.com/n00bware/android_apps_parchment
Here is my code (from example http://www.anddev.org/novice-tutorials-f8/splash-fade-activity-animations-overridependingtransition-t9464.html):
public class SplashScreen extends Activity {
private static final int SPLASH_DISPLAY_TIME = 1500; /* 1.5 seconds */
private static final String TAG = "Parchment";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Create a new handler with which to start the main activity
and close this splash activity after SPLASH_DISPLAY_TIME has
elapsed. */
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent parchment = new Intent(SplashScreen.this, ParchmentActivity.class);
SplashScreen.this.startActivity(parchment);
SplashScreen.this.finish();
overridePendingTransition(R.anim.fade_main_in, R.anim.fade_splash_out);
}
}, SPLASH_DISPLAY_TIME);
}
/* I found a suggestion to try overriding onConfigurationChanged()
but it doesn't stop a new timer from being started */
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
/* I also tried overriding onStart() but this also doesn't stop a
new timer. What exactly is called when an orientation configuration
changes happens? */
#Override
public void onStart() {
super.onStart();
setContentView(R.layout.splash);
}
you can create a new static boolean, set it false and in on create do the Handler action only if the flag is false...
PS: inside the if statement, you must set the boolean flag to true :)
Good luck!
Create the handler in onCreate, release it in onDestroy, send a message / post a runnable in onStart, remove message / runnable in onStop.
This will reset the timer with each rotate, so you could potentially keep the splash screen up if you rotated the device every second.
In Android it can take a second or so to switch rotations, you probably want this behaviour because it would be possible to start app, rotate and not see the splash.
First off, I did post this to the android google group first but its moderated and I'm not sure how long itll take to show up there so hoping someone here can help.
I created a simple app with a ListView following the ListActivity
examples I found on the net.
The app has 2 activities with the first having a button to create the
second. When i hit the close button on the second activity I would
like it to release its memory (or at least allow it to be garbage
collected). Currently it will never release.
I must be doing something wrong here because the MyListActivity never
gets released. Can anyone tell me if I am doing something wrong with
the way my activities are created/destroyed? or if my usage of the
ListView is wrong?
Thanks.
My App as a zip - http://www.mediafire.com/?l26o5hz2bmbwk6j
Screen Shot of Eclipse MAT showing the list activity never releasing
memory - www.mediafire.com/?qr6ga0k
public class MyListActivity extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listlayout);
ListAdapter ada = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, GENRES);
setListAdapter(ada);
}
#Override
public void onDestroy()
{
super.onDestroy();
System.gc();
}
public void ClickHandler(View target)
{
switch (target.getId())
{
case R.id.LL_Btn1:
finish();
break;
}
}
private static final String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};}
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void ClickHandler(View target)
{
switch (target.getId())
{
case R.id.M_Button01:
Intent intent = new Intent();
intent.setClassName(MyListActivity.class.getPackage().getName(), MyListActivity.class.getName());
startActivity(intent);
break;
}
}}
Have you tried getting rid of System.gc() and see what happens? Calling System.gc() is merely telling the VM to initiate garbage collection - there is no guarantee when it will be done.
Not sure if this will help, and its not generally recommended to do this, but the following will kill your process in onDestroy():
System.runFinalizersOnExit(true);
android.os.Process.killProcess(android.os.Process.myPid());
Put that in the onDestroy method. Now this may actually kill your main process if called from a child activity (havn't tested it).
WARNING: I generally don't recommend doing this as its a "hackly" way of doing it, but if you just need your app (or activity) to close upon exit then this will work. You still need to debug to find out why your app is staying open and eating memory.
Eclipse MAT was the cause. New Android Studio doesn't cause these problems.
I have a problem and that is my SplashScreen I have. It is built as an intro and after 3 seconds it shows the main menu of the program.
Anyway, if I press down Back or Home button during the time the SplashScreen shows, it closes, but the activity I have chosen to follow after the SplashScreen will still run after the three seconds.
My code: *****UPDATED CODE*****
Handler ur = new Handler();
myRun = new Runnable() {
public void run() {
mainIntent = new Intent(SplashScreen.this,MyApp.class);
SplashScreen.this.startActivity(mainIntent);
SplashScreen.this.finish();
overridePendingTransition(R.anim.fadein,
R.anim.fadeout);
}
};
ur.postDelayed(myRun, SPLASH_DISPLAY_TIME);
}
protected void onStop() {
super.onStop();
ur.removeCallbacks(myRun);
}
Even if I have an onStop() in this SplashScreen, the next activity will still run after the SPLASH_DISPLAY_TIME.
Since I changed the code I got Force Close after I pressed the Home button and the SplashScreen disappeared, also, I cannot launch my second activity.
Try to do it this way:
private static final int SPLASH_DISPLAY_TIME = 3000;
Handler ur = new Handler();
Runnable yourRunnable = new Runnable() {
public void run() {
mainIntent = new Intent(SplashScreen.this,MyApp.class);
SplashScreen.this.startActivity(mainIntent);
SplashScreen.this.finish();
overridePendingTransition(R.anim.fadein,
R.anim.fadeout);
}
};
ur.postDelayed(yourRunnable, SPLASH_DISPLAY_TIME);
Then, somewhere on onDestroy or whatever method you use to detect when your splashscreen activity get closed:
// somewhere
ur.removeCallbacks(yourRunnable);