한국어

Coding

온누리070 플레이스토어 다운로드
    acrobits softphone
     온누리 070 카카오 프러스 친구추가온누리 070 카카오 프러스 친구추가친추
     카카오톡 채팅 상담 카카오톡 채팅 상담카톡
    
     라인상담
     라인으로 공유

     페북공유

   ◎위챗 : speedseoul


  
     PAYPAL
     
     PRICE
     

pixel.gif

    before pay call 0088 from app


https://stackoverflow.com/questions/36457564/display-back-button-of-action-bar-is-not-going-back-in-android/36457747


6

I am developing an Android app. I am using ActionBar with AppCompactActivity. In my app, I add back button to action bar. But when I click on it, it is not going back to the previous activity. For example, I start activity 2 from activity 1. Activity 2 contains action bar with back button. But when I click on action bar back button of activity 2, it is not going back to activity 1.

This is how I set action bar for activity 2:

public class EditProfileActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_profile);
        Toolbar toolbar = (Toolbar)findViewById(R.id.profile_action_toolbar);
        setSupportActionBar(toolbar);
        setTitle("Edit Profile");
        ActionBar actionBar= getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

This is how I started activity 2 from activity 1:

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

It is not going back when I click this button

enter image description here

Why it is not going back?

29

Add the following to your activity.You have to handle the click event of the back button.

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

Here you have 2 options:

a) provide a parentActivityName to your SecondActivity tag in AndroidManifest.xml like this:

 <activity
    ...
    android:name=".SecondActivity"
    android:parentActivityName=".MainActivity" >

b) override onOptionsItemSelected in SecondActivity like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

I would suggest reading this guide for more information.

1

Here is your code

 public class EditProfileActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.edit_profile);
            Toolbar toolbar = (Toolbar)findViewById(R.id.profile_action_toolbar);
            setSupportActionBar(toolbar);
            setTitle("Edit Profile");
            ActionBar actionBar= getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

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

            return super.onOptionsItemSelected(item);
        }
    }     
0

You have to override onOptionsItemSelected and check the item's id, if it is equals with home button's id, just call onBackPressed method.

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

You have to define what should happen when you click on that button, this can be done in your second activity's onOptionsItemSelected method. Notice the android.R.id.home constant which refers to the activity's back button that you want to use.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case android.R.id.home:

        finish(); //close the activty
        return true;
    }
    return super.onOptionsItemSelected(item);
}
-1

First of all, always see Android Guidelines http://developer.android.com/intl/pt-br/design/patterns/navigation.html to prevent Google blocks Android apps.

Try to add this code in your Activity

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

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

    return super.onOptionsItemSelected(menuItem);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
}
조회 수 5384
조회 수 5293
FCM 푸시 메세지 전송
admin
2019.09.27
조회 수 5648
조회 수 7447
조회 수 8016
조회 수 6419
조회 수 7258
조회 수 7783
조회 수 7326
조회 수 11208
조회 수 6709
조회 수 7810
조회 수 7097
조회 수 6937
android apk 패키징 v1, v2
admin
2018.12.05
조회 수 7475
조회 수 7186
조회 수 6943
조회 수 7078
조회 수 8018
SDK Platform Release Notes
admin
2018.05.13
조회 수 7377
sdk-tools list
admin
2018.05.13
조회 수 7383
조회 수 7841
조회 수 7764
조회 수 7280
조회 수 7416
Firebase용 Cloud 함수
admin
2018.04.26
조회 수 7783
안드로이드 알람
admin
2018.02.23
조회 수 7965
조회 수 7907
조회 수 7852
gcm 코딩 사례
admin
2018.01.09
조회 수 7723
조회 수 12419
조회 수 8124
조회 수 8775
조회 수 8163
조회 수 10226
FCM PHP Curld
admin
2018.01.01
조회 수 8695
FCM 과 GCM 차이
admin
2018.01.01
조회 수 10306
조회 수 8050