5

Tôi muốn bật nút trang chủ trong đoạn của mình. Câu hỏi này được hỏi trước đó nhưng cho một hoạt động.
tôi đã cố gắng ...Bật nút home ActionbarSherlock, Sherlockfragment

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

... nhưng điều này không hoạt động.
Đây là mã của tôi:

import com.actionbarsherlock.R; 
import com.actionbarsherlock.app.ActionBar; 
import com.actionbarsherlock.app.ActionBar.Tab; 
import com.actionbarsherlock.app.SherlockFragment; 
import com.actionbarsherlock.view.MenuItem; 

import android.support.v4.app.Fragment; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 

public class SafanTab extends SherlockFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, 
          Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.safantab, container, false); 
    } 

    public OnClickListener onOverClick = new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent myIntent = new Intent(view.getContext(), Over_Safan.class); 
      startActivityForResult(myIntent, 0); 
     } 
    }; 

    public OnClickListener onProductenClick = new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent myIntent = new Intent(view.getContext(), Over_Safan.class); 
      startActivityForResult(myIntent, 0); 
     } 
    }; 

    public OnClickListener onTwitterClick = new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent myIntent = new Intent(view.getContext(), Over_Safan.class); 
      startActivityForResult(myIntent, 0); 
     } 
    }; 

} 

Làm thế nào bạn có thể kích hoạt một nút home trên SherlockFragment?

Trả lời

13

Bạn đã thử thêm mã bên dưới vào Hoạt động chính - một hoạt động chính là extends SherlockActivity hoặc extends SherlockFragmentActivity?

ActionBar actionBar = getSupportActionBar(); 
actionBar.setDisplayHomeAsUpEnabled(true); 

Tôi không nghĩ bạn có thể getSupportActionBar trong thứ gì đó chỉ extends SherlockFragment.

+0

vâng! nó làm việc bạn chỉ cần lưu lại ngày của tôi: P! – user1534326

+4

Nhưng 'SherlockFragment' không cho phép bạn gọi' getSherlockActivity(). GetSupportActionBar() '. –

25

Bạn cũng cần phải ghi đè lên các lựa chọn menu tùy chọn:

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

Hãy ghi nhớ, các mã trên sẽ chạy trong một hoạt động (do đó finish()). Nếu bạn không sử dụng Activity (điều này có thể lạ với tôi ...), thì bạn sẽ cần phải thay thế điều đó.

+0

nhưng nó đang đưa ra một lỗi tại "getSupportActionbar (0)" – user1534326

+1

... sau đó nói rằng trong bài đăng gốc của bạn. Hãy thử thay đổi nó thành 'getSherlockActivity(). GetSupportActionBar()'. – Eric

+0

Cảm ơn câu trả lời nhưng mattdonders đã đưa ra câu trả lời đúng. – user1534326

0

Giải pháp đơn giản nhất là làm theo ba bước đơn giản:

1 - Khai báo trong AndroidManifest đó là cha mẹ của hoạt động của bạn:

<activity android:theme="@style/Theme.MyApp" 
      android:name="com.example.CurrentActivity" 
      android:parentActivityName="com.example.MainActivity" > 
     <!-- Parent activity meta-data to support 4.0 and lower --> 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.ParentActivity" /> 

2 - Thêm biểu tượng Up in the action bar , chỉ cần gọi từ phương pháp onCreate của bạn:

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

3 - Xác định hoạt động để mở khi người dùng nhấn vào nút Up:

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

LƯU Ý: Nếu bạn gọi số (theo đề nghị của @ Eric):

finish(); 

thay vì:

NavUtils.navigateUpFromSameTask(this); 

của bạn không thực sự thực hiện "lên navigation", kể từ khi số documentation nói:

Điều hướng lên khác với điều hướng quay lại do nút Back của hệ thống cung cấp. Nút Quay lại được sử dụng để điều hướng theo trình tự thời gian đảo ngược thông qua lịch sử màn hình mà người dùng đã làm việc gần đây. Nó thường dựa trên các mối quan hệ thời gian giữa các màn hình, thay vì cấu trúc phân cấp của ứng dụng (đó là cơ sở cho việc điều hướng).

Các vấn đề liên quan