getSupportFragmentManager(); instance - java

I am using 3 classes to reuse how the fragments should be placed in the screen. The First class is generic abstract class that are used to add the fragment to the transaction. The other two classes are extending the abstract class with an implementation for the fragment.
The First Class:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
public abstract class SingleFragmentActivity extends FragmentActivity {
protected abstract Fragment createFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}
}
2nd Class
import android.support.v4.app.Fragment;
public class CrimeActivity extends SingleFragmentActivity {
#Override
protected Fragment createFragment() {
return new CrimeFragment();
}
}
3rd Class
import android.support.v4.app.Fragment;
public class CrimeListActivity extends SingleFragmentActivity{
#Override
protected Fragment createFragment() {
return new CrimeListFragment();
}
}
With the above, does i am using the same reference of getSupportFragmentManager(); that was already defined in the abstract generic class, or each one of the 2nd and 3rd Class get their own references. How Java really works here?

Am I using the same reference of getSupportFragmentManager();?
Yes, that is how class inheritance works in Java. FragmentActivity#getSupportFragmentManager() is the same method in all your classes.

Related

Simple swipe screen using VIewPager2 (Java)

I am trying to create layout which will show three screens which could be changed by swipe or by button. Between those buttons should be dots indicator of selected page.
I spent many time searching of samples how to do it, but usually I stucked on some issue. I don't need option of dynamical adding of fragments. The best and working solution was thisone, but I wasn't able to create three different fragments and it was missing the dots indicator.
Could someone help me with this issue? Thank you very much.
you can just have a new class that extends to FragmentStateAdapter and set the class as the adapter of your viewpager like so:
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
ViewPager2 viewPager2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager2 = findViewById(R.id.viewpager2);
viewPager2.setAdapter(new ViewPagerFragmentAdapter(this));
}
static class ViewPagerFragmentAdapter extends FragmentStateAdapter {
public ViewPagerFragmentAdapter(#NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
#NonNull
#Override
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new Vertical1();
case 1:
return new Vertical2();
case 2:
return new Vertical3();
}
return new Vertical2();
}
#Override
public int getItemCount() {
return 3;
}
}
}
You can create a adapter by extending from RecyclerView.Adapter() and use simple layouts(not fragments) for every page and set that adapter to viewpager2.adapter = adapter

Fragments and getFragmentManager and version compatibility

I am fairly new to coding and have looked everywhere but may not be looking correctly. I am having a problem with the placeholders. Not sure to have the support or not for the fragment manager being it is v7 support
Error:(21, 32) error: no suitable method found for add(int,ListFragment)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment) is not applicable
(argument mismatch; ListFragment cannot be converted to Fragment)
Here is my code
package chris.smellslikebacon;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements ListFragment.OnRecipeSelectedInterface {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListFragment savedFragment = (ListFragment) getSupportFragmentManager().findFragmentById(R.id.placeholders);
if(savedFragment == null) {
ListFragment fragment = new ListFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.placeholders, fragment);
fragmentTransaction.commit();
}
}
#Override
public void onListRecipeSelected(int index) {
Toast.makeText(MainActivity.this, Recipes.names[index], Toast.LENGTH_SHORT).show(); }
}
I think you should use getSupportFragmentManager instead for support:
FragmentManager fragmentManager = getSupportFragmentManager();
Hope this helps.

Wrong 2nd argument type: calling .replace() from within fragment

I know this question is asked often but none of the accepted solutions have worked for me. (reference solution)
I'm getting the classic "Wrong 2nd argument type" error when I try to launch a Preference Menu fragment from within one of my otherfragments.
Unforunately, even after doing what the accepted answer in that question suggests (calling FragmentManager fragmentManager = getSupportFragmentManager() and importing android.support.v4.app.FragmentManager; I still see the "Wrong 2nd argument type" bug when I call .replace().
A quick overview: my MainActivity.java opens a fragment viewpager (MenuPager.java), from which I try to launch another fragment (FragmentSettingsMenu.java) when a button is clicked within FragmentTrackRecordMenu.java (a child fragment of MenuPager.java).
My code is below. I try to launch the settings menu fragment when the menu button (within FragmentTrackRecord) is clicked but I can't get around the aforementioned compiler error. How can I display my PreferencesFragment successfully from this onClick?
MainActivity.java
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
public static ViewPager menuPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
menuPager = (ViewPager) findViewById(R.id.pager);
PagerAdapter pageAdapter = new MenuPagerAdapter(getSupportFragmentManager());
menuPager.setAdapter(pageAdapter);
...
}
}
MenuPagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MenuPagerAdapter extends FragmentPagerAdapter {
public MenuPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
return new FragmentNeckDisplayMenu();
case 1:
return new FragmentCapoMenu();
case 2:
return new FragmentTrackRecordMenu();
default:
break;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
FragmentTrackRecordMenu.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
public class FragmentTrackRecordMenu extends Fragment {
private Button menuIcon;
public FragmentTrackRecordMenu(){ } //default constructor
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.menu_fragment_recorder, container, false);
menuIcon = (Button) v.findViewById(R.id.menuIcon);
menuIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { //open popup window
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(android.R.id.content, new FragmentSettingsMenu()) //COMPILER ERROR
.commit();
}
});
}
}
FragmentSettingsMenu.java
public class FragmentSettingsMenu extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from the XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
getSupportFragmentManager() returns the fragment manager of android.support.v4.app.FragmentManager and your FragmentSettingMenu.java is of android.app.Fragment. That's why "Wrong 2nd argument type" error because they are incompatible.
your FragmentManager is of v4 type and FragmentSettingsMenu is not a v4 type.Hence it does not allow.Try the below line in activity.
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefsFragment()).commit();
.try this link here
it may give you some help.

Different behavior of android.app.Fragment and android.support.v4.app.Fragment while using backstack

I've created Activity and added a fragment to it using FragmentManeger. When i use android.app.Fragment and press the back button my application closes. When i use android.support.v4.app.Fragment and press the back button, the fragment is removed from the activity, but application is still working. I can't really understand why is that happening.
Here is the code i used:
Activity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.replace(R.id.content_fragment, new Fragment1())
.addToBackStack("first")
.commit();
}
}
Fragment:
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
}
When i simply replace import in Activity and Fragment to same classes, but in the support library, the result is different...
EDIT:
I also replaced getFragemetManeger() to getSupportFragmentMeneger() and it still works different
if you're only looking to change the back behavior, you can try overriding
onBackPressed with something like:
if(backstackCount() > 0)
{
super.onBackPressed
}
The issue occured because i used android.support.v7.app.AppCompatActivity and android.app.Fragment. I should've used android.app.Fragment with android.app.Activity or AppCompatActivity with support fragment.

Cannot make a static reference to the non-static method show(FragmentManager, String) from the type DialogFragment

Hey Guys i'm having a few issues with I think importation. I'm getting the error message in the title under MyDialog.show(manager, "MyDialog");
Anybody know what it means? I'm thinking some issue with importing but i'm not to sure.
Rest of code:
package com.example.sub_assignment1_2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
/**
* A simple {#link Fragment} subclass.
*
*/
public class FragmentB extends Fragment implements MyDialog.Communicator {
public FragmentB() {
// Required empty public constructor
}
public void showDialog(View v)
{
FragmentManager manager=getFragmentManager();
MyDialog myDialog=new MyDialog();
MyDialog.show(manager, "MyDialog");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_b, container, false);
}
#Override
public void onDialogMessage(String message) {
Toast.makeText(this, message,Toast.LENGTH_SHORT).show();
}
}
I'm also having an issue under Toast.makeText(this, message,Toast.LENGTH_SHORT).show(); The displayed error is: Toast.makeText(this, message,Toast.LENGTH_SHORT).show(); Which i'm also thinking is an import error?
If anybody could help that would be great.
In MyDialog.show(manager, "MyDialog");
should you not have myDialog.show - ie the instance not the class? Calling the method on the class name would be a static reference, which doesn't require an instance.

Categories