Nik
Nik

Reputation: 33

Android Toolbar back button not being recognised

I am working on an the settings screens for an android appliation.

The settings screen contains 2 'sub screens' both have a toolbar and a have a back button.

The back button on screen 1 does nothing The back button on screen 1 works perfectly and returns me to the main settings screen (the 'hard' back button works for both screens)

Screen 1 (Preferences Screen):

Screen 1 (Preferences Screen)

Screen 1 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_preferences"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.clear.pocketcross.Preferences">

    <Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/colorPrimary"
        android:popupTheme="@android:style/ThemeOverlay.Material.Light"
        android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <Switch
            android:id="@+id/counterscroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:defaultValue="true"
            android:key="counter_scroll"
            android:shadowColor="@color/gradient_mid"
            android:shadowDx="5"
            android:shadowDy="5"
            android:text="@string/scrollCounters"
            android:thumb="@drawable/pocketcross_btn_radio_on_holo_light" />

        <Switch
            android:id="@+id/navswipe"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:defaultValue="true"
            android:key="nav_swipe"
            android:shadowColor="@color/gradient_mid"
            android:shadowDx="5"
            android:shadowDy="5"
            android:text="@string/swipeTitle"
            android:thumb="@drawable/pocketcross_btn_radio_on_holo_light" />
    </LinearLayout>

</RelativeLayout>

Java for Screen 1:

Switch nav;
Switch scroll;
SharedPreferences shared;
public static final String MyPREFERENCES = "PocketCross";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_preferences);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //Toolbar will now take on default Action Bar characteristics
    setActionBar(toolbar);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    this.setTitle("Preferences");

    nav = (Switch) findViewById(R.id.navswipe);
    scroll = (Switch) findViewById(R.id.counterscroll);

    if (shared == null) {
        shared = this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    }
    nav.setChecked(shared.getBoolean("nav_swipe", true));
    scroll.setChecked(shared.getBoolean("counter_scroll", true));

    nav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            SharedPreferences.Editor spEdit = shared.edit();
            spEdit.putBoolean("nav_swipe", isChecked);
            spEdit.apply();
        }
    });

    scroll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            SharedPreferences.Editor spEdit = shared.edit();
            spEdit.putBoolean("counter_scroll", isChecked);
            spEdit.apply();
        }
    });
}

Screen 2 (Pocket Cross Parameters Screen):

Screen 2 (Pocket Cross Parameters Screen)

XML for Screen 2:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parametersTable"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="*"
    android:stretchColumns="*"
    android:background="@color/background"
    android:clickable="false">

    <Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/colorPrimary"
        android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar"
        android:popupTheme="@android:style/ThemeOverlay.Material.Light" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/parametersList"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_below="@+id/toolbar" />

</RelativeLayout>

Java for Screen 2: -

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paramaters);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //Toolbar will now take on default Action Bar characteristics
        setActionBar(toolbar);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        this.setTitle(R.string.parametersTitle);

        mntFile = MainMenu.mntFile;

        fileData = xmlTools.getXmlFile(mntFile);
        tags = fileData.xmlTags;
        for (int i = 0; i < tags.size(); i++) {
            tag = tags.get(i);
            if (tag.nodeTag.equalsIgnoreCase("PocketCrossSysParam")) {
                nodes = tag.nodeData;
                paramSetting = xmlTools.getNodeFromList(nodes, "ParamName");
                paramValue = xmlTools.getNodeFromList(nodes, "ParamValue");
                parameter = new PocketCrossParameter(paramSetting,paramValue);
                parameters.add(parameter);
            }
        }
        loadParams();
    }

(loadParams is a function to get the various parameters from an xml file)

I have tried adding the following to my code but still the back button does nothing: -

@Override
public boolean onOptionsItemSelected(MenuItem item) { 
        switch (item.getItemId()) {
        case android.R.id.home: 
            onBackPressed();
            return true;
        }

    return super.onOptionsItemSelected(item);
}

What I am struggling to understand is why the back button on every single screen in my app works perfectly except this one.

I'm guessing it will be something obvious but I have no idea what.

Upvotes: 2

Views: 1760

Answers (3)

Chaitanya Atkuri
Chaitanya Atkuri

Reputation: 1672

The issue seems to be with your xml file. You have specified your parent layout as clickable false and hence not getting your click calls. Try removing it.

Upvotes: 1

Santosh Bharati
Santosh Bharati

Reputation: 71

You can check the following

1) Your activity extend AppCompatActivity
2)

Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

3) override the following method

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()== android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

Upvotes: 1

elbert rivas
elbert rivas

Reputation: 1464

In your manifest.xml add this line, android:parentActivityName=".(Name of the activity you want to return)"

Just like this

<activity
            android:name=".(CurrentActivity)"
            android:parentActivityName=".(Activity to be returned)"/>

Upvotes: 2

Related Questions