Android - Back button in the title bar

前端 未结 26 2063
南方客
南方客 2020-12-04 07:02

In many apps (Calendar, Drive, Play Store) when you tap a button and enter a new activity, the icon in the title bar turns into a back button, but for the app I am making, i

相关标签:
26条回答
  • 2020-12-04 07:17

    First you need to write this codes

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    

    Then add this line in manifest

     <activity android:name=".MainActivity"
                android:parentActivityName=".PreviousActivity"></activity>
    

    I think it will work

    0 讨论(0)
  • 2020-12-04 07:18
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.YourxmlFileName);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
      public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
    
            if (id==android.R.id.home) {
                finish();
                return true;
            }
            return false;
        }
    
    0 讨论(0)
  • 2020-12-04 07:19

    After a quality time I have found, theme option is the main problem in my code And following is the proper way to show the toolbar for me

    In AndroidManifest file first you have to change your theme style

    Theme.AppCompat.Light.DarkActionBar
    to 
    Theme.AppCompat.Light.NoActionBar
    

    then at your activity xml you need to call your own Toolbar like

    <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:id="@+id/toolbar"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:elevation="4dp"/>
    

    And then this toolbar should be called in your Java file by

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    

    And for toolbar showing U should check the null to avoid NullPointerException

    if(getSupportActionBar() != null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    

    For Home activity back add this

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

    OR for your desired activity back

    public boolean onOptionsItemSelected(MenuItem item){
        Intent myIntent = new Intent(getApplicationContext(), YourActivity.class);
        startActivityForResult(myIntent, 0);
        return true;
    }
    
    0 讨论(0)
  • 2020-12-04 07:19

    This is working for me getSupportActionBar().setDisplayHomeAsUpEnabled(false); enter image description here

    0 讨论(0)
  • 2020-12-04 07:20

    If your activity does extend Activity

    public class YourActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_xxx);
    
            getActionBar().setHomeButtonEnabled(true);
    
            [...]
        }
    
        [...]
    }
    

    If your action extends AppCompatActivity

    public class YourActivity extends AppCompatActivity {
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_xxx);
    
                getSupportActionBar().setHomeButtonEnabled(true);
    
                [...]
            }
    
            [...]
        }
    

    Nothing more to do, See Add up action

    [OPTIONAL] To explicitly define parent activity modify your Manifest.xml like this:

    <application ... >
        ...
        <!-- The main/home activity (it has no parent activity) -->
        <activity
            android:name="com.example.myfirstapp.MainActivity" ...>
            ...
        </activity>
        <!-- A child of the main activity -->
        <activity
            android:name="com.example.myfirstapp.YourActivity "
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.myfirstapp.MainActivity" >
            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
        </activity>
    </application>
    

    See Specify the Parent Activity

    0 讨论(0)
  • 2020-12-04 07:20

    You need to add the below mentioned code in the manifest file. Search for the activity in which you want to add the back arrow functionality. If you find the one then fine or create the activity

    <activity android:name=".SearchActivity">
    
    </activity>
    

    Then add the following three lines of code in between.

    <meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value="com.example.raqib.instadate.MainActivity" />
    

    And don't forget to add this piece of code in onCreate(); method of your particular activity in which you need back arrow.

            Toolbar toolbar = (Toolbar) findViewById(R.id.searchToolbar);
        setSupportActionBar(toolbar);
        try{
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }catch(NullPointerException e){
           Log.e("SearchActivity Toolbar", "You have got a NULL POINTER EXCEPTION");
        }
    

    This is how i solved the problem. Thanks.

    0 讨论(0)
提交回复
热议问题