한국어

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();
}
조회 수 5359
조회 수 5263
FCM 푸시 메세지 전송
admin
2019.09.27
조회 수 5616
조회 수 7402
조회 수 7972
조회 수 6381
조회 수 7214
조회 수 7746
조회 수 7282
조회 수 11166
조회 수 6661
조회 수 7765
조회 수 7054
조회 수 6896
android apk 패키징 v1, v2
admin
2018.12.05
조회 수 7434
조회 수 7145
조회 수 6898
조회 수 7029
조회 수 7976
SDK Platform Release Notes
admin
2018.05.13
조회 수 7334
sdk-tools list
admin
2018.05.13
조회 수 7337
조회 수 7804
조회 수 7725
조회 수 7231
조회 수 7377
Firebase용 Cloud 함수
admin
2018.04.26
조회 수 7745
안드로이드 알람
admin
2018.02.23
조회 수 7918
조회 수 7868
조회 수 7805
gcm 코딩 사례
admin
2018.01.09
조회 수 7680
조회 수 12376
조회 수 8078
조회 수 8731
조회 수 8128
조회 수 10181
FCM PHP Curld
admin
2018.01.01
조회 수 8649
FCM 과 GCM 차이
admin
2018.01.01
조회 수 10261
조회 수 8003