问题
I am using ActionBarSherlock, I am not able to go to class that extends SherlockFragment from activity
I need to move from Activity to fragment class
Here is my Activity class
Intent notificationIntent = new Intent(context,MessagesFragment.class);
And the Fragment class is like
public class MessagesFragment extends SherlockFragment implements
OnItemClickListener {
// Layout parameters declaration
private PullToRefreshListView lv_messages;
private ImageView iv_no_data;
private LinearLayout ll_bg;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getSherlockActivity().getSupportActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM);
getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(
true);
getSherlockActivity().getSupportActionBar().setHomeButtonEnabled(true);
getSherlockActivity().getSupportActionBar().setDisplayShowHomeEnabled(
true);
getSherlockActivity().getSupportActionBar().setCustomView(
R.layout.header);
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#009fe3")));
TextView txt = (TextView) getActivity().findViewById(
R.id.tv_title_header);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"georgia.ttf");
txt.setText("MESSAGES");
txt.setTypeface(font);
return inflater.inflate(R.layout.listview_refreshable, null);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
.
.
.
.
}
If I use switchfragment
method it shows lots of errors in FragmentChangeActivity
private void switchFragment(Fragment fragment) {
if (getActivity() == null)
return;
if (getActivity() instanceof FragmentChangeActivity) {
FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
fca.switchContent(fragment);
}
}
回答1:
You need to created a class that extends FragmentActivity and start yourfragment there
public class MessagesFragmentActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new MessagesFragment ()).commit();}
}
}
Your fragment constructor.
public YourFragment() {
}
then from you calling activity, start your fragment activity in the normal way
Intent i = new Intent(YourActivity.this,MessagesFragment.class);
startActivity(i);
回答2:
Use FragmentTransaction
to go to any Fragment you want. If you have several Fragments, this method will switch between them.
Here is direction:
public enum FragmentsAvailable {
HISTORY
}
public class MyActivity extends FragmentActivity
...
private void changeFragment(Fragment newFragment, FragmentsAvailable newFragmentType, boolean withoutAnimation) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
try {
getSupportFragmentManager().popBackStackImmediate(newFragmentType.toString(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
} catch (java.lang.IllegalStateException e) {
}
transaction.addToBackStack(newFragmentType.toString());
transaction.replace(R.id.fragmentContainer, newFragment);
transaction.commitAllowingStateLoss();
getSupportFragmentManager().executePendingTransactions();
}
回答3:
You can not do that. Because every Fragment was contained in a Activity, So you can just jump to a Activity which contains that Fragment.
回答4:
A fragment is Attached to an activity, you can Add a fragment or Replace a fragment with FragmentTransition. Note that a fragment need an activity to exist !
You don't go from activity to fragment... But if you are in an activity that contain a fragment you can open a new activity on top of the first.
来源:https://stackoverflow.com/questions/18011374/how-to-go-to-fragment-from-activity