Background Info: I currently have an AppCompatActivity which is working. But right now, everything is in the onCreate method. I am concerned it is causing some little bugs I've been having lately with my PagerAdapter having java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! The bugs only happen when I switch activities back and forth very fast. I wonder if fixing this will fix the bug.
System requirements: Min SDK: 15, Target SDK: 23 (at the time of this writing, or whatever is most current)
Question: How do I move my user interface initialization code into the onCreateView method?
I am familiar with how to implement the onCreate method of android.support.v4.app.Fragment, like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_songdetail, container, false);
rootView.findViewById(R.id.info).setVisibility(View.INVISIBLE);
return rootView;
}
But there are no options like that for AppCompatActivity. These are my options, from the AppCompatActivity docs:
View onCreateView(String name, Context context, AttributeSet attrs)
View onCreateView(View parent, String name, Context context,
AttributeSet attrs)
Here is my code:
(I omitted the methods besides onCreate and onCreateView)
public class DealPage extends AppCompatActivity
{
private Deal deal;
private Poll poll;
private Video video;
#Override
protected void onCreate(Bundle savedInstanceState)
{
RealmDatabase.setRealmInstance(this);
//----------- UNPACK EXTRAS -----------
String date = getIntent().getExtras().getString(KeyStrings.EXTRA_DATE);
//---------------- PREREQUISITE INITIALIZATION ----------
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deal_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_with_spinner);
setSupportActionBar(toolbar);
//------------ Enable "UP" navigation ---------------
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getDataFromDatabase(date);
//----------------- THEMES AND COLORS ------------------
//Set up colors
final int backgroundColor = this.deal.getTheme().getBackgroundColor().getColor();
final int accentColor = this.deal.getTheme().getAccentColor().getColor();
String themeForeground = this.deal.getTheme().getForeground();
final int foreground = generateForegroundColor(themeForeground);
final String foregroundWebView = generateForegroundWebViewString(themeForeground); // This is necessary because HTML does not have an alpha channel.
//Set toolbar colors
toolbar.setBackgroundColor(accentColor);
toolbar.setTitleTextColor(backgroundColor);
//Set Page Background Color
RelativeLayout dealPageBackground = (RelativeLayout) findViewById(R.id.deal_page_background);
dealPageBackground.setBackgroundColor(backgroundColor);
//----------- INITIALIZE THE ACTUAL DEAL PAGE STUFF ----------------
//Title
TextView title = (TextView) findViewById(R.id.title);
title.setText(this.deal.getTitle());
title.setTextColor(foreground);
//Price
TextView price = (TextView) findViewById(R.id.price);
NumberFormat fmt = NumberFormat.getCurrencyInstance();
price.setText(fmt.format(this.deal.getItems().first().getPrice()));
price.setTextColor(foreground);
//ViewInBrowser
setUpViewInBrowserButton(backgroundColor, accentColor);
AndDown andDown = new AndDown();
//Set up "linkColorHTML"
String linkColorHTML = generateLinkColorHTML(accentColor);
//Features
setUpFeaturesView(andDown, backgroundColor, linkColorHTML, foregroundWebView);
//More Specs button
setUpMoreSpecsButton(backgroundColor, foreground, (Spinner) findViewById(R.id.spinner));
//Story Title
TextView storyTitle = (TextView) findViewById(R.id.story_title);
storyTitle.setText(this.deal.getStory().getTitle());
storyTitle.setTextColor(accentColor);
//Story Body
setUpStoryBody(andDown, backgroundColor, linkColorHTML, foregroundWebView);
//Specs Title
TextView specsTitle = (TextView) findViewById(R.id.specs_title);
specsTitle.setText(this.deal.getTitle());
specsTitle.setTextColor(accentColor);
//Specs
setUpSpecificationsView(andDown, backgroundColor, linkColorHTML, foregroundWebView);
//Set up ViewPager
ViewPager viewPager = (ViewPager) findViewById(R.id.photos_view_pager);
viewPager.setAdapter(new PhotoPagerAdapter(this, this.deal.getPhotos()));
// Setup spinner
setUpSpinner(accentColor, backgroundColor, toolbar);
}
#Override
public View onCreateView(String name, Context context, AttributeSet attrs)
{
return super.onCreateView(name, context, attrs);
}
}
You must use a PlaceholderFragment.
Try this:
public class RestaurantListActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment(), "fragmentName").commit();
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {...}
Related
i know that i can use a recycler view to show a list of item but what if I wanna show a list of existing Fragments?
I'll try to explain better with an example:
In my app I have a fragment called review that's look like this:
public class ReviewView extends Fragment implements ReviewViewContract.View {
private TextView tvContent, tvName,tvDate,tvReactionCounter, tvCommentCountCounter;
private ImageView userPhoto;
private ImageButton btnMore;
private Button btnComment, btnLike;
private PopupMenu popup;
private ReviewViewContract.Presenter presenter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.component_review, container, false);
presenter = new ReviewViewPresenter(this,getContext(),getArguments());
initComponent(v);
presenter.getDetail();
return v;
}
//Some view methods...
#Override
public void initComponent(View view) {
userPhoto = view.findViewById(R.id.img_userphoto_profile);
tvName = view.findViewById(R.id.tv_name_profile);
tvDate = view.findViewById(R.id.tvdate_profile);
tvContent = view.findViewById(R.id.content_review_component);
tvReactionCounter = view.findViewById(R.id.tv_reactionCounter);
tvCommentCountCounter = view.findViewById(R.id.tv_commentsCounter);
btnComment = view.findViewById(R.id.btn_comment);
btnLike=view.findViewById(R.id.btn_like);
popup= new PopupMenu(view.getContext(), btnMore);
popup.getMenuInflater()
.inflate(R.menu.menu_review, popup.getMenu());
btnMore = view.findViewById(R.id.more_review);
popup = new PopupMenu(view.getContext(), btnMore);
popup.getMenuInflater()
.inflate(R.menu.menu_review, popup.getMenu());
btnMore.setOnClickListener(v->popup.show());
}
#Override
public void setDetail(String title, String date, String authorImageURL, String overView) {
tvName.setText("ciao");
}
private void likeBtnListener(){...};
private void commentsBtnListener(){...};
}
Now I need to show an indefinite number of reviewView in my activity, I thought to use a recycler view to do that but I do not want to rewrite all methods of ReviewView in the viewHolder class,(it doesn't seem a smart thing to do) so what should I do?
PS: I know that I can archive this with something like
Review fragment = new ReviewView();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
But this way doesn't seem the best way to do this.
MainActivity on startup add a fragment layout to Relativeview, then i send a data to fragment to add it to ExpandablelistView but my app shows me error that couldn't recognize ExpandablelistView.
MainActivity:
public class MainActivity extends AppCompatActivity implements FragmentAddCatergory.onClickButtonListener {
private FragmentManager manager;
private FragmentTransaction transactionShowList;
private FragmentTransaction transactionAddCatergory;
private FragmentAddCatergory addCatergory;
private FragmentShowCategory showCategory;
private boolean addcategory;
private TextView txtAddCategory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
transactionShowList = manager.beginTransaction();
showCategory = new FragmentShowCategory();
addCatergory=new FragmentAddCatergory();
transactionShowList.add(R.id.Fragment_container, showCategory);
transactionShowList.commit();
addcategory=false;
txtAddCategory = (TextView) findViewById(R.id.txtaddcategory);
txtAddCategory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ChangeFragment();
}
});
}
public void ChangeFragment(){
transactionAddCatergory=manager.beginTransaction();
if (addcategory){
transactionAddCatergory.replace(R.id.Fragment_container,addCatergory);
txtAddCategory.setText("Do you want to see your List?Show me!");
addcategory=false;
}else{
transactionAddCatergory.replace(R.id.Fragment_container,showCategory);
txtAddCategory.setText("Do you want to add a Category?Create One");
addcategory=true;
}
transactionAddCatergory.commit();
}
#Override
public void ClickButton(String group, String child) {
FragmentShowCategory a=new FragmentShowCategory();
a.showExpand(this,group,child);
}}
in last above code i make object from first fragment and send a data and in below code is code of first fragment
public class FragmentShowCategory extends Fragment {
private View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.activity_expandable_list_view, container, false);
return view;
}
public void showExpand(Context context, String g, String c) {
Toast.makeText(context, g + " is " + c, Toast.LENGTH_LONG).show();
HashMap<String, List<String>> carsDetails = DataProvider.getInfo(g, c);
List<String> carsBrands = new ArrayList<String>(carsDetails.keySet());
ItemClass adapter = new ItemClass(context, carsDetails, carsBrands);
ExpandableListView list = (ExpandableListView) view.findViewById(R.id.expandList);
list.setAdapter(adapter);
}}
but when i ran my app, i get error that i don't know why in line of:
ExpandableListView list = (ExpandableListView) view.findViewById(R.id.expandList);
i'd appreciate to help me.
Your fragment's view hierarchy is not inflated automatically just because you created an instance of your fragment, as you do in ClickButton. The onCreateView() method that has to be called first in order to inflate your views is part of the fragment's lifecycle. You should let Android instantiate your fragment, and acquire it's instance through the FragmentManager.
This tutorial explains basics about fragments very well.
I'm more googling to find how can i pass simple view as an object to fragment, but i can't.
for example in MainActivity i have simple view as :
TextView text = (TextView) findviewById(R.id.tv_text);
now i want to pass that to fragment. this below code is my attach Fragment on MainActivity
MainActivity :
public void attachFragment() {
fts = getActivity().getFragmentManager().beginTransaction();
mFragment = new FragmentMarketDetail();
fts.replace(R.id.cardsLine, mFragment, "FragmentMarketDetail");
fts.commit();
}
and this is my Fragment:
public class FragmentMarketDetail extends Fragment implements ObservableScrollViewCallbacks {
public static final String SCROLLVIEW_STATE = "scrollviewState";
private ObservableScrollView scrollViewTest;
private Context context;
private int scrollY;
public static FragmentMarketDetail newInstance() {
FragmentMarketDetail fragmentFirst = new FragmentMarketDetail();
return fragmentFirst;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_online_categories, container, false);
scrollViewTest = (ObservableScrollView) view.findViewById(R.id.scrollViewTest);
scrollViewTest.setScrollViewCallbacks(this);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity().getBaseContext();
}
}
It wouldn't be good practise to pass a view that way. If you want to access the view in your activity from within your fragment class, use getActivity() to access the activity to which your fragment is attached, and from there you find your TextView.
TextView text = (TextView) getActivity().findViewById(R.id.tv_text);
How about adding a set function in your custom fragment
e.g.
public void setTextView(TextView tv){
this.tv = tv
}
and then calling it after
mFragment = new FragmentMarketDetail();
mFragment.setTextView(textView)
Find fragment by tag and invoke a function on it:
mFragment = (FragmentMarketDetail ) getActivity().getFragmentManager().findFragmentByTag(FragmentMarketDetail .class.getSimpleName());
mFragment.passTextView(textView);
Of course fragment must be added to backstack.
I have created an external class, NotesView, which extends View for implementation in my MainActivity.
This View requires information passed from the MainActivity, so its constructor takes an ArrayList of Note objects.
public class NotesView extends View {
private ArrayList<Note> notes = new ArrayList<>();
public NotesView(Context context, ArrayList<Note> notes) {
super(context);
this.notes = notes;
}
In my MainActivity, I used the following code to display this view: (Trying to add a CustomView in the Design tab of the layout does not work as I cannot supply the ArrayList parameter)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notesView = new NotesView(this, noteList);
setContentView(notesView);
}
Unfortunately, I am now not able to add any objects at all through the Design view of the layout, I assume this is because I have used setContentView. I do not wish to add all my components programmatically, is there a way around this?
Calling setContentView replaces the whole view for your layout. That means if you call setContentView twice, whatever was added to the screen from the first call is overridden and no longer accessible.
There are multiple answers to your question, here is a pragmatic one:
What is inside R.layout.activity_main? Let's assume there is a FrameLayout / LinearLayout / RelativeLayout with id root
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup rootView = (ViewGroup) findViewById(R.id.root);
notesView = new NotesView(this, noteList);
rootView.addView(notesView);
}
Another choice, you could also take your custom view to have a setter if you wish:
public class NotesView extends View {
private final List<Note> notes = new ArrayList<>();
public NotesView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void replaceNotes(List<Note> notes) {
this.notes.clear();
this.notes.addAll(notes);
}
Then you can add this view in your XML file (R.layout.activity_main) and call the setter method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotesView notesView = (NotesView) findViewById(R.id.notes);
notesView.replaceNotes(noteList);
}
You can add a setter function to your NotesView class:
public class NotesView extends View {
private ArrayList<Note> notes;
public NotesView(Context context) {
super(context);
}
public void setNotes(ArrayList<Note> notes) {
this.notes = notes;
}
}
And then set it in the main activity:
NotesView notesView = (NotesView) findViewById(R.id.yourNotesView);
notesView.setNotes(noteList);
By the way I recommend Butterknife to cast views in your layout without the verbose findViewByIds, declarations, onXListeners, etc.
I am using tab host with fragment, the following is the code of the main activity
public class HomeActivity extends Activity{
private FragmentTabHost mTabHost;
private ArrayList<CustomTabIndicator> mCustomTabIndicator;
private ArrayList<BaseFragment> mTabFragments;
private class CustomTabIndicator {
private int mIdResId;
private int mTitleResId;
private int mIconResId;
public CustomTabIndicator(int idResId, int titleResId, int iconResId) {
this.mIdResId = idResId;
this.mTitleResId = titleResId;
this.mIconResId = iconResId;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
initializeTabIndicatorsAndFragments();
addTabIndicatorsToTabHost();
}
private void initializeTabIndicatorsAndFragments() {
mCustomTabIndicator = new ArrayList<HomeActivity.CustomTabIndicator>();
mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_dashboard,
R.string.tab_dashboard, R.drawable.tab_dashboard));
mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_feed,
R.string.tab_feed, R.drawable.tab_feed));
mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_lists,
R.string.tab_lists, R.drawable.tab_lists));
mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_me,
R.string.tab_me, R.drawable.tab_me));
mTabFragments = new ArrayList<BaseFragment>();
mTabFragments.add(new DashboardFragment());
mTabFragments.add(new FeedFragment());
mTabFragments.add(new ListsFragment());
mTabFragments.add(new MeFragment());
}
private void addTabIndicatorsToTabHost() {
for (int i = 0; i < mCustomTabIndicator.size(); i++) {
mTabHost.addTab(
mTabHost.newTabSpec(
getString(mCustomTabIndicator.get(i).mIdResId))
.setIndicator(
createTabView(
this,
mCustomTabIndicator.get(i).mTitleResId,
mCustomTabIndicator.get(i).mIconResId)),
mTabFragments.get(i).getClass(), null);
}
}
#SuppressLint("InflateParams")
private View createTabView(final Context context, final int textStringId,
final int imageResId) {
View view = LayoutInflater.from(context).inflate(
R.layout.fragment_tab_header_image_text_layout, null);
ImageView tabIV = (ImageView) view.findViewById(R.id.tab_icon);
tabIV.setImageResource(imageResId);
//TextView titleTV = (TextView) view.findViewById(R.id.tab_title);
//titleTV.setText(textStringId);
return view;
}
}
This is the code of one of the fragements
public class MeFragment extends BaseFragment {
private View mFragementView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i("AMIRA", "MeFragment - onCreateView");
mFragementView = inflater.inflate(R.layout.fragment_me_screen,container, false);
initializeUIComponents();
initializeUIComponentsData();
initializeUIComponentsTheme();
initializeUIComponentsAction();
return mFragementView;
}
}
The problem now that onCreateView called every time I change the tab, and take long time to render and draw the content of fragment.
So I have tried the following code
public class MeFragment extends BaseFragment {
private View mFragementView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mFragementView != null) {
return mFragementView;
} else {
Log.i("AMIRA", "MeFragment - onCreateView");
mFragementView = inflater.inflate(R.layout.fragment_me_screen,container, false);
initializeUIComponents();
initializeUIComponentsData();
initializeUIComponentsTheme();
initializeUIComponentsAction();
return mFragementView;
}
}
}
and i got the following exception
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
so can anyone help here ?
First, about the java.lang.IllegalStateException,
You haven't initialized mFragmentView.
Say something like:
mFragmentView = (View)getActivity().findViewById(R.id.mFragmentView);
or:
mFragmentView = (View)getActivity().findFragmentById(R.id.mFragmentView);
Second about the Fragment changing every time you change the tab.
Try this:
add setRetainInstance(true); to the Fragments onAttach() or onCreateView().
Open for correction, as always!
Regards,
Edward Quixote.