I am making a news app where I have a fragment that holds all the news in a list and another fragment that have videos list. When you touch a new it will call a method from the activity named openNewsCont that will replace the current fragment with another one.
The proble I am having is when I call that method I get a blank screen, I have looked many other codes but I couldn't find the solution for my problem.
Here is the method I call from the MainActivity.java
public void openNewsCont(String text) {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.pager, new NewsContent() );
ft.addToBackStack(null);
ft.commit();
fm.executePendingTransactions();
}
Here is the Tab1.java that hold the news and calls the method from the MainActivity.java
package com.example.link_test;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class Tab1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View android = inflater.inflate(R.layout.tab1, container, false);
LinearLayout newLink = (LinearLayout) android.findViewById(R.id.news1);
newLink.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String text = "Text from fragment";
MainActivity ma = (MainActivity) getActivity();
ma.openNewsCont(text);
}
});
return android;
}
}
And here is the fragment that should be called and shows a blank screen NewsContent.java
package com.example.link_test;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class NewsContent extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_content, container, false);
return view;
}
}
replace fragment as:
Fragment feeddetailFragment = new FeedDetailFragment();
feedFragment.getFragmentManager().beginTransaction()
.replace(R.id.frame_container, feeddetailFragment,Constant.FRAGMENT_FEEDDETAIL)
.addToBackStack(null)
.commit();
It will work..
If Still not working then try to check in the fragment which you want to show after replacement whether that fragment's view is null or not
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_business_details,
container, false);
initializeViewObjects();
setListeners();
} catch (InflateException e) {
return view;
}
return view;
}
One obvious thing that sticks out to me is that you have ft.addToBackStack(null); twice. Pretty sure it should only be in there once.
Additionally, I believe you need to call fm.executePendingTransactions() directly after ft.commit().
I solved the problem by removing the support for v4 of fragments since that is only required by my TabAdapter but only for the fragments that are represented in tabs like news,vide,etc. From the fragments that aren't managed as tabs such as NewsContent.java
here is how my method to open another fragment from Tab1 should've been.
public void openNewsCont() {
NewsContent nc = new NewsContent();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, nc);
ft.addToBackStack(null);
ft.commit();
}
And here is how the NewsContent should be:
package com.example.link_test;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class NewsContent extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View android = inflater.inflate(R.layout.news_content, container, false);
return android;
}
}
Thanks to Eagle11 for his effort to help me with this nooby question that took me a whole morning to fix. I hope this answer helps another newbies like me.
you can have this code on the activity that hosts the fragments. It checks your stack and relaunches the fragment. That's one of the ways I decided to handle the issue on my end.
@Override
public void onBackPressed() {
int fragments = getFragmentManager().getBackStackEntryCount();
Toast.makeText(this, String.valueOf(fragments), Toast.LENGTH_LONG).show();
if (fragments == 0) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, new NewsContent());
fragmentTransaction.commit();
super.onBackPressed();
}
}
来源:https://stackoverflow.com/questions/23391350/android-getting-white-screen-with-fragment-transaction