I want to launch a new fragment to view some data. Currently, I have a main activity that has a bunch of actionbar tabs, each of which is a fragment. So, within a tab fragment, I have a button, chartsButton. I have my onclicklistener all set for it, and here's the onClick method:
public OnClickListener chartsListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent chartFragment = new Intent();
startActivity(chartFragment);
}
};
Now, as I said, this listener is within a class that extends Fragment. So, I want to launch a new fragment (chartsFragment) via the intent to replace the whole screen. When the user clicks back, it'll bring them back to the tabs and main activity. Here's my chart fragment:
public class chartsFragment extends Fragment {
public View onCreateView() {
//LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return (inflater.inflate(R.layout.chartfragment, null));
}
}
The current error I am dealing with: "android.content.ActivityNotFoundException: No Activity found to handle Intent { }". That's fine, I understand that I could use getActivity().startActivity(chartsFragment), but that results in the same error. I suppose what I am looking for here, is how do I launch an intent from within a fragment that results in opening a new fragment?
5条答案
按热度按时间c0vxltue1#
你的问题的答案很简单:使用新的
Fragment
替换当前的Fragment
并将事务推送到后备堆栈。这将保留“后退”按钮的行为...创建一个新的
Activity
确实挫败了使用片段的整个目的无论如何...非常适得其反。http://developer.android.com/guide/components/fragments.html#Transactions
ycggw6v22#
您无法打开新片段。片段需要始终由Activity托管。如果片段位于同一Activity中(例如标签页),那么后退键导航将很棘手。我假设您想用该片段打开一个新屏幕。
因此,您只需创建一个新Activity,并将新片段放入其中,然后Activity将通过Activity类显式或通过
intent filter
s隐式地对Intent做出React。y1aodyip3#
试试这个它可能会帮助你:
jogvjijk4#
试试这个它可能会帮助你:
syqv5f0l5#
试试这个:
在第二个活动中:
在包含该片段的FisrtActivity中: