我有一个fragment
(我们称之为 MyFragment ),根据参数中传递的参数膨胀不同的布局。
如果从另一个片段启动 MyFragment ,则一切正常。但是,如果 MyFragment 处于活动状态且我想使用不同的布局参数启动新的 MyFragment ,则fragmentManager
根本不会创建新的片段。
data.setInt("Layout index",i);
fragmentTab0 = (Fragment) new MyFragment();
fragmentTab0.setArguments(data);
fragmentTransaction.replace(R.id.fragmentContent, fragmentTab0, "MY");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
我如何强制说服fragmentTransaction
再次启动片段?
注意:这里的重点是我需要再次充气布局,这与之前充气的布局不同。代码如下:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
switch( getArguments().getInt("Layout index") ) {
case 1:
rootView = inflater.inflate(R.layout.firstlayout, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.secondlayout, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.thirdlayout, container, false);
break;
default: break;
}
答案 0 :(得分:3)
绕过解决方案
解释(胡佛看到它)
由于fragmentTransaction.replace/add/remove
的源代码是 不可用我找不到真正发生的事情。但它是 有理由认为它在某种程度上比较了当前的阶级 带有替换类名的名称,如果是,则退出 相同。击>。感谢@devconsole指出了源代码 码。我现在知道为什么会这样。FragmentManager.removeFragment()
方法不重置片段状态,它保持RESUMED,然后 方法moveToState(CREATED)
仅启动片段if (f.mState < newState)
=if (RESUMED < CREATED)
=false
。否则, ergo ,它只是恢复片段。
因此,为了解决这个问题,我创建了一个几乎为空的片段,其目的只是用目标片段替换自己。
public class JumpFragment {
public JumpFragment() {
}
@Override
public void onStart() {
Bundle data = getArguments();
int containerId = data.getString("containerID");
String tag = data.getString("tag");
//Class<?> c = data.get???("class");
//Fragment f = (Fragment) c.newInstance();
Fragment f = (Fragment) new MyFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
f.setArguments(data);
fragmentTransaction.replace(containerId, f, tag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
我用它:
data.setInt("Layout index",i);
data.setInt("containerID",R.id.fragmentContent);
data.setString("tag","MY");
fragmentTab0 = (Fragment) new JumpFragment();
fragmentTab0.setArguments(data);
fragmentTransaction.replace(R.id.fragmentContent, fragmentTab0);
fragmentTransaction.commit();
现在没有任何片段被相同的类片段替换:
MyFragment - &gt; JumpFragment - &gt; MyFragment
我还没弄清楚如何通过参数bundle传递类,使其完全通用。
答案 1 :(得分:1)
以下对我没有任何问题。请注意,我使用了Android支持库。
activity_main.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ONE" />
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button_one"
android:text="TWO" />
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/button_one" >
</FrameLayout>
</RelativeLayout>
MainActivity.java
:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment fragment = DetailsFragment.newInstance("INITIAL");
transaction.add(R.id.main_container, fragment);
transaction.commit();
}
findViewById(R.id.button_one).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
update("Button 1 clicked");
}
});
findViewById(R.id.button_two).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
update("Button 2 clicked");
}
});
}
protected void update(String value) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment fragment = DetailsFragment.newInstance(value);
transaction.replace(R.id.main_container, fragment);
transaction.commit();
}
public static final class DetailsFragment extends Fragment {
public static DetailsFragment newInstance(String param) {
DetailsFragment fragment = new DetailsFragment();
Bundle args = new Bundle();
args.putString("param", param);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView textView = new TextView(container.getContext());
textView.setText(getArguments().getString("param"));
return textView;
}
}
}
答案 2 :(得分:0)
您是否尝试先使用remove(fragMgr.findFragmentByTag("MY"))
删除片段,然后添加新片段?
PS:我假设您没有对此片段进行任何引用。
答案 3 :(得分:0)
如果我理解正确:要替换当前正在显示的内容的片段以及用户是否会使其重新显示?
如果这是正确的,那么就这样做了类似的事情:
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View V = inflater.inflate(R.layout.myLayout, container, false);
// Call method that fills the layout with data
displayData(V);
// Put a listener here that checks for user input
Button redisplayButton = (Button) V.findViewById(R.id.my_button);
// if the button is clicked....
redisplayButton.setOnClickListener(new OnClickListener() {
public void onClick(View v){
//
// do some stuff
//
// ....then eventually...
displayData(V);
}
});
return V;
}
稍后您可以使用displayData()方法定义片段显示的内容....
public void displayData(View V){
// Do something
return;
}
希望这有帮助!