Finish an Activity After a Time Period

Finish an activity after a time period

Since you also want to show the countdown, I would recommend a CountDownTimer. This has methods to take action at each "tick" which can be an interval you set in the constructor. And it's methods run on the UI Thread so you can easily update a TextView, etc...

In it's onFinish() method you can call finish() for your Activity or do any other appropriate action.

See this answer for an example

Edit with more clear example

Here I have an inner-class which extends CountDownTimer

@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.some_xml);
// initialize Views, Animation, etc...

// Initialize the CountDownClass
timer = new MyCountDown(11000, 1000);
}

// inner class
private class MyCountDown extends CountDownTimer
{
public MyCountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
frameAnimation.start();
start();
}

@Override
public void onFinish() {
secs = 10;
// I have an Intent you might not need one
startActivity(intent);
YourActivity.this.finish();
}

@Override
public void onTick(long duration) {
cd.setText(String.valueOf(secs));
secs = secs - 1;
}
}

how to close activity after x minutes?

Handler handler = new Handler();
Runnable r=new Runnable() {
@Override
public void run() {
finish();
}
};
handler.postDelayed(r, 2000);

Close an Activity after 10 seconds?

After 100 MS, the activity will finish using the following code.

public class Message_Note extends Activity 
{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message);

Handler handler = new Handler();

handler.postDelayed(new Runnable() {
public void run() {
finish();
}
}, 100);
}
}

close an activity after some time

you can use this:

 Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
image.setVisibility(View.GONE);
// If you want to call Activity then call from here for 5 seconds it automatically call and your image disappear....
}
}, 5000);

Close activity after x minutes of inactive

Here is a code of Activity that will automatically close itself after five seconds.

public class TestActivity extends ActionBarActivity {

private static final int DELAY = 5000;

public boolean inactive;

Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
if(inactive) {
finish();
} else {
mHideHandler.postDelayed(mHideRunnable, DELAY);
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_annotation_test);
mHideHandler.postDelayed(mHideRunnable, DELAY);

}
}

Now all you need to do is to change inactive variable to true after user interaction. Which will cancel activity finish and start new countdown.

Finish all activities at a time

Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish.
to do so apply the following code in ur project

Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

The above code finishes all the activities except for FirstActivity.
Then we need to finish the FirstActivity's
Enter the below code in Firstactivity's oncreate

if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}

and you are done....

How to finish an activity before is being displayed?

In this case, you can create a SplashActivity where you can check user's state

class SplashActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
//show a splash image and check user's state then redirect to correct screen
}
}

How to finish an activity when transition is done Android

Guys found a solution to this,
instead of using this code to the transition

if (Build.VERSION.SDK_INT >= 21) {
TransitionInflater inflater = TransitionInflater.from(LoginActivity.this);
Transition transition = inflater.inflateTransition(R.transition.fade_transition);
TransisitonTime = transition.getDuration()*2;
getWindow().setExitTransition(transition);
}

ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(LoginActivity.this);

i used this instead

overridePendingTransition(R.animator.fade_in,
R.animator.fade_out);

with these xml files

fade_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="2000"/>
</set>

fade_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="2000"/>
</set>

this way i can do the fade in and fade out transition when the finish() is called

kudos to @Droidman for pointing me in the right direction

What is Activity.finish() method doing exactly?

When calling finish() on an activity, the method onDestroy() is executed. This method can do things like:

  1. Dismiss any dialogs the activity was managing.
  2. Close any cursors the activity was managing.
  3. Close any open search dialog

Also, onDestroy() isn't a destructor. It doesn't actually destroy the object. It's just a method that's called based on a certain state. So your instance is still alive and very well* after the superclass's onDestroy() runs and returns.Android keeps processes around in case the user wants to restart the app, this makes the startup phase faster. The process will not be doing anything and if memory needs to be reclaimed, the process will be killed



Related Topics



Leave a reply



Submit