How do the back button of the actionBar function? [closed]

0

I'm not getting it to work. pv help me. My manifest looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alinesilvagonzaga.testedostrespontinhos">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Main2Activity"
        android:label="page two"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
        />

    </activity>
</application>

</manifest>

And the method to check that it has been touched:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
     }
      return super.onOptionsItemSelected(item);
  }
    
asked by anonymous 12.12.2016 / 01:30

2 answers

0

Create an Intent for the home case Example:

case android.R.id.home:             Intent intent = new Intent (Main2Activity this, MainActivity.class); startActivity (); return true;             return true;

    
13.12.2016 / 01:30
3

Use the setNavigationIcon method to add the icon to Toolbar and setNavigationOnClickListener to perform an action by clicking the icon.

  

Java 8 enabled method enabled

    applicationToolbar.setNavigationIcon(R.drawable.ic_close_26dp);
    applicationToolbar.setNavigationOnClickListener(view -> finish());

Note : The lambda method view->finish() only works if you have Java 8 enabled.

  

Method with versions Java 8

    applicationToolbar.setNavigationIcon(R.drawable.ic_close_26dp);
    applicationToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // ação desejada...

            }
        });
    
12.12.2016 / 04:24