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;
}