Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close an Activity after 10 seconds?

I use it to call another activity

Main.java

 Intent intent = new Intent(this, Message_Note.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);

Message_Note.java :

public class Message_Note extends Activity {

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



}

How can i CLOSE the Message_Note Activity after 10 seconds ?? i should use a thread ?

like image 241
NPLS Avatar asked Aug 07 '13 12:08

NPLS


People also ask

How do I know if my activity is finished?

Using activity. isFinishing() is the right one solution. it return true if activity is finished so before creating dialog check for the condition. if true then create and show dialog.

How do you end an intent?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do I stop activity from another activity?

In Activity [A], on button click, I am calling Activity [B] without finishing Activity [A]. Now in Activity [B], there are two buttons, New and Modify. When the user clicks on modify then pop an activity [A] from the stack with all the options ticked..


4 Answers

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);
    }
}
like image 110
Spring Breaker Avatar answered Oct 05 '22 05:10

Spring Breaker


You can use following approach.

Approach 1

int finishTime = 10; //10 secs
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        YourActivity.this.finish();
    }
}, finishTime * 1000);

Approach 2

int FinishTime = 10;
int countDownInterval = 1000; 
counterTimer = new CountDownTimer(FinishTime * 1000, countDownInterval) {
    public void onFinish() {
        //finish your activity here
    }

    public void onTick(long millisUntilFinished) {
        //called every 1 sec coz countDownInterval = 1000 (1 sec)
    }
};
counterTimer.start();
like image 33
Chintan Rathod Avatar answered Oct 05 '22 06:10

Chintan Rathod


You can use AlarmManager. See :

http://developer.android.com/reference/android/app/AlarmManager.html

and

Alarm Manager Example

like image 45
Paul-Mehdy M'Rabet Avatar answered Oct 05 '22 05:10

Paul-Mehdy M'Rabet


Another way is just like this:

new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
            Message_Note.this.finish();
        }
    }, 10000);
like image 32
kalin Avatar answered Oct 05 '22 04:10

kalin