Question

I have a menu and when clicked on a menu, it goes inside another menu. for example, mainmenu-->click on airport guide --> menu2 but when i am in menu2 and press on the back button, the app is closing instead of going back to mainmenu . i am not able to figure out the problem here. i am new to android development

package com.shashank.sharjahinternationalairport;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends Activity {
ImageButton flightInfoButton;
ImageButton airportGuideButton;
ImageButton visitorInfoButton;
ImageButton saaDcaButton;
ImageButton cargoButton;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

flightInfoButton = (ImageButton) findViewById(R.id.flightInfo);
airportGuideButton = (ImageButton) findViewById(R.id.airportGuide);
visitorInfoButton = (ImageButton) findViewById(R.id.visitorInfo);
saaDcaButton = (ImageButton) findViewById(R.id.saaDca);        
cargoButton = (ImageButton) findViewById(R.id.cargo);

airportGuideButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View V){

    setContentView(R.layout.airport_guide);

  }
 });
  visitorInfoButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View V){

        setContentView(R.layout.visitor_info);

    }
  });
  saaDcaButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View V){

        setContentView(R.layout.saa_dca);

     }
     });
   }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

No correct solution

OTHER TIPS

You are using setContentView, so you are just changing the view layout. When you press the backbutton, the Android calls onBackPressed, which the default implementation is to call finnish() method, that closes the activity.

You can override the onBackPressed method to set the other view.

@Override
public void onBackPressed()
{
     //setContentView the previous view
}

Hope it helps!

you can always override the onBackPressed method of Activity, you'll have something like this

public class MainActivity extends Activity {
    ImageButton flightInfoButton;
    ImageButton airportGuideButton;
    ImageButton visitorInfoButton;
    ImageButton saaDcaButton;
    ImageButton cargoButton;

    private enum VIEWS {
        VIEW_1, VIEW_2, VIEW_3
    };

    private VIEWS mSelectedView = VIEWS.VIEW_1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        flightInfoButton = (ImageButton) findViewById(R.id.flightInfo);
        airportGuideButton = (ImageButton) findViewById(R.id.airportGuide);
        visitorInfoButton = (ImageButton) findViewById(R.id.visitorInfo);
        saaDcaButton = (ImageButton) findViewById(R.id.saaDca);
        cargoButton = (ImageButton) findViewById(R.id.cargo);

        airportGuideButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View V) {
                showView1();
            }
        });
        visitorInfoButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View V) {
                showView2();
            }
        });
        saaDcaButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View V) {
                showView3();
            }
        });
    }

    private void showView1() {
        setContentView(R.layout.airport_guide);
        mSelectedView = VIEWS.VIEW_1;
    }

    private void showView2() {
        setContentView(R.layout.visitor_info);
        mSelectedView = VIEWS.VIEW_2;
    }

    private void showView3() {
        setContentView(R.layout.saa_dca);
        mSelectedView = VIEWS.VIEW_3;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onBackPressed() {
        switch (mSelectedView) {
        case VIEW_1:
            super.onBackPressed();
            break;
        case VIEW_2:
            showView1();
            break;
        case VIEW_3:
            showView2();
            break;
        }
    }
}

All you is doing that you are changing the Content View of the class MainActivity you need to create sperate classes for your difreent layouts and you can call it by INTENT

This is happennig because you are using only one activity, and on click just changing the content view. Even though this appears like a multi page activity, it is only one screen. Hence onBackPressed() it closes. Yo will have to create different activiies for each of your menu options and start them using Intents on Button Click.

Intent i = new Intent(this, SecondActivity.class); startActivity(i);

You are using only one activity, if it is many activity mean you can track back to previously loaded activity.

else you must needed only one activity mean please store the layouts in some array, then in the onBackPressed() you can it get LIFO manner

  @Override

public void onBackPressed() {


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top