0

I want to have a condition for the tab when i backpressed it all goes on Home tab but when i backpressed all the activities end.

here's my code. try to help me.

 backpressed = true;

   if (results.equals("HOME")) {
        mTabHost.setCurrentTab(0);
        backpressed = false;
    } else if (results.equals("B")) {
        mTabHost.setCurrentTab(2);
    } else if (results.equals("C")) {
        mTabHost.setCurrentTab(3);
    } else if (results.equals("D")) {
        mTabHost.setCurrentTab(1);
    } else if (results.equals("E")) {

        singleton.openNewsFeed = true;
        mTabHost.setCurrentTab(4);
    } else {
        singleton.openMessage = true;
        mTabHost.setCurrentTab(4);
    }

and this is my onBackPressed

 @Override
public void onBackPressed() {
    /*super.onBackPressed();*/

    if (backpressed == true) {
        Intent intent = new Intent(getApplicationContext(), Dashboard.class);
        intent.putExtra("result", "HOME"); // getText() SHOULD NOT be static!!!
        startActivity(intent);
    }else{
        //FINISH
       super.onBackPressed();
    }

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jam
  • 91
  • 12
  • use this `Your_Activity.super.onBackPressed();` rather than using Intent. refer to this https://stackoverflow.com/questions/6413700/android-proper-way-to-use-onbackpressed-with-toast – L2_Paver Oct 14 '19 at 02:23

2 Answers2

0

You have a wrong condition here

You should try with this

 backpressed = false;

   if (results.equals("HOME")) {
        mTabHost.setCurrentTab(0);
        backpressed = true;
    } else if (results.equals("B")) {
        mTabHost.setCurrentTab(2);
    } else if (results.equals("C")) {
        mTabHost.setCurrentTab(3);
    } else if (results.equals("D")) {
        mTabHost.setCurrentTab(1);
    } else if (results.equals("E")) {

        singleton.openNewsFeed = true;
        mTabHost.setCurrentTab(4);
    } else {
        singleton.openMessage = true;
        mTabHost.setCurrentTab(4);
    }

on back pressed

 @Override
public void onBackPressed() {
    /*super.onBackPressed();*/

    if (backpressed == true) {
        Intent intent = new Intent(getApplicationContext(), Dashboard.class);
        intent.putExtra("result", "HOME"); // getText() SHOULD NOT be static!!!
        startActivity(intent);
    }else{
        //FINISH
       //super.onBackPressed();
       mTabHost.setCurrentTab(0);
    }

}
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36
  • not the Home page. the Home screen of android phone is what i was talking about – Jam Oct 14 '19 at 06:05
0

I can not Understand Clearly what you do from onBackPressed(),

But i think this onBackPressed() code helps you :-

MainActivity.Java

//Codes for Exit Dialog Box Starts
 @Override
 public void onBackPressed(){
     AlertDialog.Builder builder=new AlertDialog.Builder(this);
     builder.setMessage("Are You Sure You Want to Exit ?")
             .setCancelable(false)
             .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialogInterface, int i) {
                     MainActivity.super.onBackPressed();
                 }
             })
             .setNegativeButton("No", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialogInterface, int i) {
                     dialogInterface.cancel();
                 }
             });
     AlertDialog alertDialog = builder.create();
     alertDialog.show();
 }
 //Codes for Exit Dialog Box Ends
S Kumar
  • 242
  • 3
  • 12