0

I want to close my application from splash screen automatically after showing an message if there is no internet connection available or any error occurs due to response error. My code closes the application but cant close the splash screen.Times of India(TOI) application does like this. How to implement this feature.

my splash screen activity is like this..

public class SplashScreen extends Activity {

// Splash screen timer
private static int SPLASH_TIME_OUT = 8000;
static String MENU = null;
ArrayList<String> ls = new ArrayList<String>();
private String[] categoryType;
private boolean flag = true;

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

    // requesting data for menu items in navigation drawer 

    String url = "http://guwahatinow.com/?json=get_category_index";
    if (isOnline()) {
        JsonObjectRequest jsonObjReqMenu = new JsonObjectRequest(Method.GET,
                url, null, new Response.Listener<JSONObject>() {

            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArrayMenu= response.getJSONArray("categories");
                    Log.d("request", "menu");
                    int loop;
                    ls.add("Top Stories");
                    for (loop = 0; loop <jsonArrayMenu.length() ; loop++) {

                        JSONObject jsonObj = (JSONObject) jsonArrayMenu.get(loop);
                        String category =jsonObj.getString("title") ;
                        //menu.add(category);
                        ls.add(loop+1, category);
                        Log.d("menu added", category);
                        Log.d("element in ls", ls.get(loop));
                    }

                    ls.add("Exit");

                    int i = ls.size();
                    categoryType = new String[i];
                    for (int j = 0; j < i; j++) {
                        categoryType[j] = ls.get(j);
                    }

                }catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },  
        new Response.ErrorListener()  {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "Please check your internet connection and try again...", Toast.LENGTH_LONG).show();
                VolleyLog.d("menu error", "Error: " + error.getMessage());
                flag = false;
                //finish();
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
                //System.exit(0);

            }
        });


        RequestQueue menuQueue = Volley.newRequestQueue(this);
        menuQueue.add(jsonObjReqMenu);

        if (flag) {
            new Handler().postDelayed(new Runnable() {

                /*
                 * Showing splash screen with a timer. This will be useful when you
                 * want to show case your app logo / company
                 */


                @Override
                public void run() {
                    // This method will be executed once the timer is over
                    // Start your app main activity

                    Intent i = new Intent(SplashScreen.this, MainActivity.class);
                    i.putExtra(com.hamburger.menu.SplashScreen.MENU, categoryType);
                    startActivity(i);

                    Log.d("main activity called", "true");
                    // close this activity
                    finish();
                }
            }, SPLASH_TIME_OUT);

        } else {
            Toast.makeText(getApplicationContext(), "Internet connection error...", Toast.LENGTH_LONG).show();
            finish();
            System.exit(0);
        }
        /*new Handler().postDelayed(new Runnable() {


         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company



            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity

                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                i.putExtra(com.hamburger.menu.SplashScreen.MENU, categoryType);
                startActivity(i);

                Log.d("main activity called", "true");
                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);*/

    } else {
        Toast.makeText(getApplicationContext(), "Please connect to internet...", Toast.LENGTH_LONG).show();

    }
}

protected boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else {
        return false;
    }
}

}
Buddy
  • 10,874
  • 5
  • 41
  • 58
Rumpa
  • 11
  • 1
  • 1
    Include just enough code to allow others to reproduce the problem. For help with this, read How to create a [Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – Jordi Castilla Sep 16 '15 at 10:48

3 Answers3

0

Calling the finish() method on an Activity when there is no connection available in your case or any other at where you want to close app.

Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35
0

In you major else block of if block for checking internet call finish() method. eg

You have you code as

if (isOnline()) {

//  your Code for calling API and starting timer 

} else {
  Toast.makeText(getApplicationContext(), "Please connect to internet...", Toast.LENGTH_LONG).show();
}

Change it to

if (isOnline()) {

//  your Code for calling API and starting timer 

} else {
  Toast.makeText(getApplicationContext(), "Please connect to internet...", Toast.LENGTH_LONG).show();
  finish();
}
Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
0

Hi please replace your code with below code where you used handler thread

CountDownTimer m_countDownTimer;
m_countDownTimer = new CountDownTimer(8000,1000) {
            @Override
            public void onTick(long millisUntilFinished)
            {

            }

            @Override
            public void onFinish()
            {
                if(!isFinishing())
                {
                    Intent i = new Intent(SplashScreen.this, MainActivity.class);
                    i.putExtra(com.hamburger.menu.SplashScreen.MENU, categoryType);
                    startActivity(i);

                    finish();
                }
            }
        }.start();

Cancel CountDownTimer object in OnDestroy()

@Override
    protected void onDestroy()
    {
        if(m_countDownTimer != null)
        {
            m_countDownTimer.cancel();
        }
        super.onDestroy();
    }