-1

I have a problem with an app of mine and I do not know what to do to fix it. I'm new to Android and do not understand java.

The app consists of multiple pages containing lists, all with a spinner to navigate between those pages. You select in item in the spinner and the app opens that page. As well as hopping between pages there is, in the spinner, an option to 'Exit App'.

Page hopping works no problem. The problem I have is with the 'Exit App' code. When I exit the app by pressing the home key on my device, or by tapping Exit App (from the spinner) and then restart the app, it opens not on the dashboard, but on the page I was last viewing. To resolve this issue, I need to long press the device's home key and then swipe the app from the recently used list.

Is there a way so that when I exit the app by pressing the home key on the device, or by tapping Exit App from the spinner and then restart the app, it does open on the dashboard?

Can anyone help?

Thanks.

The String Code...

<string-array name="TheList">
   <item>Tap Here For List…</item>
   <item>Cheese</item>
   <item>Planets</item>
   <item>Cars</item>
   <item><b>Back To Dashboard…</b></item>
   <item><b>EXIT APP</item>
</string-array>

The case numbers (1-4) in the code below are the pages that open with a spinner item selection.

Case 5 exits the app. I assume there is an error in the code that prevents the app from restarting on the dashboard.

The java code...

package com.example.acer.MyKitList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

public class Carrying extends Activity {

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


   // Spinner element & Spinner click listener
   Spinner Spinner = (Spinner) findViewById(R.id.Spinner1);

   Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


       @Override
       public void onItemSelected(AdapterView<?> arg0, View view, int position, long row_id) {

      switch (position) {
          case 1:
         Intent a = new Intent(Dashboard.this, Cheese.class);
         startActivity(a);
         finish();
         break;
          case 2:
         Intent b = new Intent(Dashboard.this, Planets.class);
         startActivity(b);
         finish();
         break;
          case 3:
         Intent c = new Intent(Dashboard.this, Cars.class);
         startActivity(c);
         finish();
         break;
          case 4:
         Intent d = new Intent(Dashboard.this, Dashboard.class);
         startActivity(d);
         finish();
         break;

          case 5:
         Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_HOME);
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
         startActivity(intent);
         System.exit(0);
         finish();

      }
       }

       @Override
       public void onNothingSelected(AdapterView<?> arg0) {
       }
   });
    }

    @Override
    public void onBackPressed() {
   finish();
    }
}
Nodens
  • 13
  • 3
  • 1
    This is Activity's default life cycle . When you press home button, Activity goes to pause state again opening it results in resume. I do not think you should mess up with default behavior. – ADM Dec 20 '17 at 10:22
  • 1
    *I'm new to Android and do not understand java.* - why don't you get some experience first? You won't get far if you don't understand java – Tim Dec 20 '17 at 10:23
  • As i have stated above You should not mess with Activity lifecycle . What you can do is handle your Exit App button . Read [This](https://stackoverflow.com/questions/6330260/finish-all-previous-activities) you will get the idea . – ADM Dec 20 '17 at 10:27
  • Thanks ADM. I have read that Android tries to discourage people from having an "exit button" as it's not good coding etiquette or something. Thanks also for the link. I found a solution there. I was missing just a couple of lines. – Nodens Dec 22 '17 at 10:58
  • No thanks to Tim, but you do get the award for being unhelpful and condescending. Well done! I wonder if you understood absolutely everything about java right from the beginning and made no mistakes... Have you never had to ask for help on something that didn't quite work? Must be nice to be all-knowing. What you could've done was say, "Nice try, you've got most of the code, but you're missing these ...." That way with your help I would have learnt something which would have turned in to experience. Ah, mockery... – Nodens Dec 22 '17 at 10:58

2 Answers2

1

The answer:

case 5:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
Nodens
  • 13
  • 3
0

put this snippet in all of your classes

@Override
protected void onResume() {
    super.onResume();
   //Start Dashboard Activity here
     Intent a = new Intent(context, Dashboard.class);
     startActivity(a);
     finish();
     }
}

This will Hard-fix the issue.

Community
  • 1
  • 1
Aqua 4
  • 771
  • 2
  • 9
  • 26
  • Thanks Aqua 4. I was missing just a couple of lines which I got from the link ADM gave, which included your - Intent a = new Intent(context, Dashboard.class); – Nodens Dec 22 '17 at 10:59
  • @Nodens now that your issue is fixed :) i'll appreciate a thumbs up & please accept the answer – Aqua 4 Dec 22 '17 at 11:01