0

I have Made a Simple Beginner Application with the help of Drawables and Intent. The Idea is making four activities and using Thread move from one to other Activities and when it reaches to the last activity, i want to close the Application Automatically, popping up a toast as "BYE" or something else. If it is possible by Intent or any other way do guide me.

Code of my Last Activity is below, but it loops to the Last activity to Infinity:

package mj.manan.thisisit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class ScreenLast extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screenlast);

        Thread t1 = new Thread(){
            public void run(){

                try {
                    Thread.sleep(2000);
                    finish();
                } catch (InterruptedException e) {
                    // Auto-generated catch block
                    e.printStackTrace();
                }finally{
                     Intent innt = new Intent(ScreenLast.this,ScreenLast.class);
                     innt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                     startActivity(innt);
                     ScreenLast.this.finish();

                }
            }
        };
        t1.start();

 }

}
Hamad
  • 5,096
  • 13
  • 37
  • 65
Manan Gupta
  • 491
  • 1
  • 7
  • 11

5 Answers5

2

To Quit Application by intent use this code :

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);

Try it.

Amresh
  • 2,098
  • 16
  • 20
0

There can be different approaches. I think you can use broadcast sender and receiver also for you case. Similar answer I posted once it may help you.

Finish an activity from another activity

Community
  • 1
  • 1
Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
0

If what I understood is Correct you want to kill end your activity when the Activity stack is not empty or to be more precise there is more than one activity in the stack.

Ill show you how I did it and you can tweak it as you like

when you reach the point you want to close activity do this:

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

then in onCreate you add :

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

you can add toast to second part or add timer or whatever youd like. the point is, first we clear all the Activity stack leaving only our current... then we finish it.

Jony-Y
  • 1,579
  • 1
  • 13
  • 30
  • It made a loop and didn't exits the App. – Manan Gupta Oct 10 '13 at 11:41
  • explain to me how could it make a loop if you clear all the stack?...so read the code... understand it and use it... i use it all the time... IT WORKS IN MY APPS ... – Jony-Y Oct 10 '13 at 15:59
0

You can use this sample code to quit an application

                        Intent intent1 = new Intent(Intent.ACTION_MAIN);
                        intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent1.addCategory(Intent.CATEGORY_HOME);
                        startActivity(intent1);
Kalai.G
  • 1,610
  • 13
  • 22
0

you are calling your same Activity :- Intent innt = new Intent(ScreenLast.this,ScreenLast.class);

either call other activity instead of this.

Or

You can also use handler for this-

@Override
    protected void onCreate(Bundle savedInstanceState) 
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screenlast);
        Toast.makeText(ScreenLast.this, "Bye", Toast.LENGTH_SHORT).show();
        move_next();
        }

public void move_next()
{
final Handler handle = new Handler();
        Runnable delay = new Runnable() {
            public void run() 
            {               
                finish();
            }
        };
        handle.postDelayed(delay,2000);
}
UchihaSasuke
  • 363
  • 2
  • 23