onCreateView() method asking for super class call - java

I have extended fragmentActivity class , as far as I read onCreateView is available in lifecycle of fragmentActivity but its not supposed to be calling super class , but its giving me error in #override asking me to call super class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
// Initializing
MarkerPoints = new ArrayList<>();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override --> error Here
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_maps,
container, false);
Button button = (Button) view.findViewById(R.id.reportButton);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Report Button Works",Toast.LENGTH_SHORT).show();
}
});
return view;
}

#Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
return super.onCreateView(parent, name, context, attrs);
}
Not
#Override --> error Here
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
You need you Fragments;
Step 1 - You need create one Fragment.
public class ExampleFragment extends Fragment {
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
//Create the view from XML layout.
final View view = inflater.inflate(R.layout.fragment_example, null);
//Perform additional configuration on layout components here.
return view;
}
}
Step 2 - Create Fragment:
public class Fragment1 extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
}}
Step 3 - Create FragmentPagerAdapter
public class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
private Fragment1 fragment1;
public PagerAdapter(FragmentManager fm) {
super(fm);
this.fragment1 = new Fragment1();
this.fragmentList = new ArrayList<>();
this.fragmentList.add(this.fragment1);
}
#Override
public Fragment getItem(int position) {
//Get fragment in list position
return fragmentList.get(position);
}
#Override
public int getCount() {
//return size of fragments list.
return fragmentList.size();
}}
Step 4 - Add FragmentPagerAdapter in your FragmentActivity:
this._viewPager.setAdapter(new PagerAdapter(this.getSupportFragmentManager()))

FragmentActivity doesn't actually have a method called onCreateView(LayoutInflater, ViewGroup, Bundle), I think you're confusing it with Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle) for which the default implementation only returns null in which case you don't actually need to call the super method.
FragmentActivity can act as a host for Fragments provided by the support library (regular Activity can only host android.app.Fragment). In your code you're using the SupportMapFragment which extends Fragment and overrides Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle) to provide its view to the hosting Activity.
For an Activity, you'd typically just set your button click listeners in onCreate after calling setContentView.

You should try overriding onCreate and changing it from protected to public.
Plus, either get rid of setContentView(R.layout.activity_maps); or change it to getActivity().setContentView(R.layout.about_main_layout);
setContentView isn't availible only in a fragment Activity's java it must have getActivity(). in front of it.
Also, you need a blank public constructor, it's required, public BlankFragment() {
// Required empty public constructor
}
Finally, to my knowledge onCreateView is fine, it's what's around it, so please consider the above posibilites.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.about_main_layout);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
// Initializing
MarkerPoints = new ArrayList<>();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this); }

FragmentActivity is used to hold a Fragment prior to API 3.0. If you're targeting an app prior to Honeycomb then you should use FragmentActivity but if you're targeting an app after Honeycomb then you should use an Activity. And remember that the FragmentActivity holds a Fragment, which means that you should attach a Fragment and call OnCreateView() and other fragment methods on the Fragment and not on FragmentActivity.
The way to attach a Fragment to FragmentActivity is
android.support.v4.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(CONTENT_VIEW_ID, myFragment).commit();
Check out this excellent SO post explaining the difference between FragmentActivity and Fragments
Difference between Fragment And FragmentActivity

Related

Button inside fragment overlap with next fragment's button

The signup page of my app is divided into three fragments. FragmentA, FragmentB and FragmentC which is attached to same activity. On clicking the next button which is present in FragmentA it will call FragmentB and same is for FragmentB and FragmentC. As I move forward the buttons of the previous fragment overlaps the current one. Below is my code. How can I avoid this situation?
MainActivity.class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signup_page);
init();
}
private void init() {
ViewPager2 viewPager = findViewById(R.id.signup_pager);
List<Fragment> fragmentList = new ArrayList<Fragment>();
FragmentA aFragment = new FragmentA();
FragmentB bFragment = new FragmentB();
fragmentList.add(aFragment);
fragmentList.add(bFragment);
SignupFragmentPagerAdapter signupFragmentPagerAdapter = new SignupFragmentPagerAdapter(this,fragmentList);
viewPager.setUserInputEnabled(false);
viewPager.setAdapter(signupFragmentPagerAdapter);
}
}
FragmentA.class
public class FragmentA extends Fragment {
EditText et_name;
Spinner sp_user_type;
Button bt_next;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fraga,container,false);
et_name = view.findViewById(R.id.editTextTextPersonName);
sp_user_type = view.findViewById(R.id.user_spinner);
bt_next = view.findViewById(R.id.button8);
bt_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentB bFragment = new FragmentB();
FragmentManager manager = getParentFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frag_a, bFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
FragmentB.class
public class FragmentB extends Fragment {
Button bt_next;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragb,container,false);
bt_next = view.findViewById(R.id.button9);
bt_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentC cFragment = new FragmentC();
FragmentManager manager = getParentFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frag_b, Fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
Cause of your problem:
You are making fragment transaction (from A to B) within Fragment A itself; and not through the placeholder of these fragments (which is in your case the ViewPager itself; So the MainActivity still sees that the ViewPager is at the first page and therefore shows Fragment A in the background.
You have two options to solve this:
First: Since you're disable page scrolling; I don't see any use to
the ViewPager .. so you can just have a fragment placeholder in the
main activity, and make fragment transaction on this placeholder.
Second: if you have a need to the ViewPager, instead of making
fragment transaction, you can use
viewPager.setCurrentItem(position) and set the position to the
target fragment according to the order you add them in the adapter.
You can do that from your fragment when you hit the button using:
((MainActivity)requireActivity()).myViewPager.setCurrentItem(position)

ListView of a Fragment (of a ViewPager inside a Fragment) that implements LoaderCallbacks<Cursor> is empty when I return back to it

The ListView inside of a Fragment that contains a ViewPager is empty when I return back to it. This ViewPager is inside of a Fragment because I'm using a Navigation Drawer Layout. I'll try to explain it in more details now:
I am using a Navigation Drawer layout and, consequently, using Fragments. In the main activity, in onCreate, I set the current Fragment with these lines of code:
#Override
protected void onCreate(Bundle savedInstanceState) {
/* More code */
currentFragment = new MainFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.layout_for_fragments, currentFragment).commit();
}
The MainFragment class contains a ViewPager that is initialized with these lines of code in the onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
/* More code */
ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.main_view_pager);
viewPager.setAdapter(new SimpleFragmentPagerAdapter(getActivity(), getActivity().getSupportFragmentManager()));
/* Code for Tablayout */
return rootView;
}
One of the Fragments of the SimpleFragmentPagerAdapter class is this one that implements LoaderManager.LoaderCallbacks
public class ExpensesFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private ExpenseCursorAdapter mExpenseCursorAdapter;
private static final int EXPENSE_LOADER = 1;
public ExpensesFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_expenses, container, false);
ListView expensesListView = (ListView) rootView.findViewById(R.id.expenses_list_view);
mExpenseCursorAdapter = new ExpenseCursorAdapter(getContext(), null);
expensesListView.setAdapter(mExpenseCursorAdapter);
getLoaderManager().initLoader(EXPENSE_LOADER, null, this);
return rootView;
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(/* arguments */);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mExpenseCursorAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
mExpenseCursorAdapter.swapCursor(null);
}
}
On the first run, everything works fine. However, if I click on the navigation drawer menu, which I'm implementing this way, the ListView becomes empty:
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_main) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().remove(currentFragment).commit();
currentFragment = new MainFragment();
fragmentManager.beginTransaction().replace(R.id.layout_for_fragments, currentFragment).commit();
} else if (id == R.id.nav_blank) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().remove(currentFragment).commit();
currentFragment = new BlankFragment();
fragmentManager.beginTransaction().replace(R.id.layout_for_fragments, currentFragment).commit();
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
The problem also exists when I rotate the device. There is a difference though. When I use the Navigation Drawer menu, none of the methods of the LoaderManager.LoaderCallbacks are called, but when I rotate the device, onLoadFinished is called.
Anyway, the problem persists. Any ideias how to solve it?
Thanks in advance.
I have found the solution!
Basically, I made two mistakes.
The first one is about the rotation of the device.
When the device is rotated, the Activity is destroyed and onCreate is called again. In this case, I should't call new MainFragment(). So, I check if the savedInstanceState is null before I call it, like this:
if (savedInstanceState == null) {
currentFragment = new MainFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().
replace(R.id.layout_for_fragments, currentFragment).commit();
}
The second one is about a fragment within a fragment. In this case, I can't simply call getSupportFragmentManager(). I should call getChildFragmentManager() instead and that's it!

Adding a view to a fragment from another fragment

I'm trying to add a view in fragment B from fragment A, they both belong to the same activity but they are never running simultaneously
My activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fm = getSupportFragmentManager();
Fragment_Main main = new Fragment_Main();
fm.beginTransaction()
.replace(R.id.mainContainer, main)
.commit();
/* Fragment_Main is a fragment with two buttons
* to either call fragment A or fragment B and
* mainContainer is a FrameLayout occupying the whole screen
*/
}
Fragment A:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_subjects, container, false);
ButterKnife.bind(this, view);
AddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentB.addMyView();
}
});
return view;
}
Fragment B:
public static void addMyView(){
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 200, 0, 0);
CustomView mView = new CustomView(mContext, "english", "a1", "10:12", "14:32");
mLayout.addView(mView, params);
// CustomView is the custom view I want to add
}
Question:
I know that a fragment can't communicate with another fragment directly, how do I tell the activity to add a view permanently to FragmentB when the method addMyView() is called?
Persisting for one activity lifecycle
FragmentA should set a flag (e.g. a boolean attribute) on the Activity instance.
When FragmentB is instantiated, the Activity should pass this flag to the created instance (e.g. via Bundle). FragmentB should then decide whether or not to add the View.
Persisting permanently
FragmentA should set a SharedPreferences flag.
FragmentB should check this flag in onCreateView and potentially add the View depending on the value.
General advice:
Solving communication between instances with static values/methods is almost always bad practice.

How to extend Activity in main class that extends "Fragment"?

I have an Android project and I use Navigation Drawer in it, and in the MainActivity, I have a switch-case, with options and your respective positions for to open when the user clicks. But, in a class called TelaQUEMSOMOS, the parameters that are there:
public class TelaQUEMSOMOS extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.telaquemsomos, container, false);
return rootView;
}
}
are extending from Fragment and not Activity. It's bad, because I need to put a Toast.makeText or findViewById, and I can't. How can I handle it?
Link for download my project(the project is in portuguese): https://goo.gl/pm5dIC
In your fragment you can call getActivity() to get instance of the activity.
Please check the code below. First we will have an activity Home.java
final String[] fragments = {
"com.locate.Establishment", "com.locate.Settings"};
ActionBarDrawerToggle mDrawerToggle;
DrawerLayout drawer;
public class Home extends Home {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
.
.//your other code here.
.
// Here I am going to call the Fragment called Establishment
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main, Fragment.instantiate(Home.this, fragments[0]));
tx.commit();
setToolbar(0);
}
}
Implementation for Fragment. Here I am going to user support Fragment V4.
public class Establishment extends android.support.v4.app.Fragment{
SupportMapFragment mapFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_establishment, null);
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
//Now will able to all you need here. By using getActivity().
}
}

Removing a fragment not working

I want to remove a fragment and show a toast when I click a textView. My code shows the toast, but doesn't remove the fragment.
My method:
public void hide(View view) {
My_frag myFrag= new My_frag();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.remove(myFrag);
transaction.commit();
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG)
.show();
}
My_frag class:
public class My_frag extends android.support.v4.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.my_frag_layout, container, false);
}
}
At some point you must create and add the Fragment correct? You are re-creating the fragment in your hide(...) method so you are trying to remove something that has never been added. Sure you may have added an instance, but not the instance you are trying to remove.
Instead, create a global variable Fragment fragToRemove in your Activity. When you create the fragment (that is where ever you do transaction.add(fragToRemove = new My_Frag);) you will hold an instance. then change your transaction.remove(myFrag) to transaction.remove(fragToRemove) and it should work just fine.
Take the instance of Fragment Transaction other than that was taken while adding fragment to activity and call remove method on that and pass the same instance of Fragment which was used at the time.
Example:
public class MainActivity extends AppCompatActivity {
FragmentTransaction fragmentTransaction;
BlankFragment blankFragment;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.activity_button);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
blankFragment=new BlankFragment(); //Fragment instance
fragmentTransaction.add(R.id.main_layout,blankFragment).commit();
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.remove(blankFragment).commit(); //created different
}
});
}
In the same way you can do in fragments also and can also remove fragment X on the click of component of fragment X.

Categories