It sounds there is a bug with this library: https://github.com/shivasurya/materialsteppers
The bug is the following: if we are in step 2, for example, and we click next, the oncreate of step 4 is called instead of oncreate of step 3. When we click next, neither the oncreate nor oncreateview of next fragment is called. Which method of the next fragment is called?
Does somebody faced the same problem?
public class ExampleActivity extends ProgressMobileStepper {
List<Class> stepperFragmentList = new ArrayList<>();
#Override
public List<Class> init() {
stepperFragmentList.add(Step1Fragment.class);
stepperFragmentList.add(Step2Fragment.class);
stepperFragmentList.add(Step3Fragment.class);
stepperFragmentList.add(Step4Fragment.class);
stepperFragmentList.add(Step5Fragment.class);
return stepperFragmentList;
}
}
Fragment 1:
public class Step1Fragment extends StepperFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onNextButtonHandler() {
return true;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return view;
}
}
Fragment 2
public class Step2Fragment extends StepperFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onNextButtonHandler() {
return true;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return view;
}
}
Fragment 3:
public class Step3Fragment extends StepperFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onNextButtonHandler() {
return true;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return view;
}
}
Fragment 4:
public class Step4Fragment extends StepperFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onNextButtonHandler() {
return true;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return view;
}
}
Fragment 5:
public class Step5Fragment extends StepperFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onNextButtonHandler() {
return true;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return view;
}
}
Related
I have DialogFragment within just some text
and I want open it in Fragment
It's Dialog Fragment first
public class subscribe_dialog_frag extends DialogFragment {
#Override
#Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);
}
}
and it's my fragment code
public class Main4Fragment extends Fragment {
private ImageButton btn_money;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main4, container, false);
btn_money = (ImageButton)view.findViewById(R.id.btn_momey);
btn_money.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment subscribe_dialog_frag = new subscribe_dialog_frag();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.btn_momey, subscribe_dialog_frag).commit();
}
});
return view;
}
}
I just open the dialogFragment.
possible?? or i do another way?
first use naming convention for classes/variables... Rename your dialog to:
public class SubscribeDialog extends DialogFragment {
#Override
#Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);
}
}
in your Main4Fragment call something like this:
btn_money.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SubscribeDialog subscribeDialog = SubscribeDialog();
subscribeDialog.show(getChildFragmentManager(),"SubscribeDialog");
}
});
I’ve been having difficulties sending data from one fragment to another when utilizing a TabLayout. I’ve read copious amounts of online posts pertaining to how to do this and have had no success when implementing them myself.
I have four fragments set up in my application. I'm trying to send data gathered from an EditText element in the fourth fragment to the TextView element of the second fragment.
I don't get any errors, but the text does not show up in the second fragment. I don't know how to proceed from here. Thanks!
Fourth Fragment (sending Fragment)
public class settings extends Fragment {
SendMessage SM;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.setl, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
final EditText inData = (EditText) view.findViewById(R.id.inMessage);
btnPassData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SM.sendData(inData.getText().toString().trim());
}
});
}
public interface SendMessage {
void sendData(String message);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
SM = (SendMessage) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException("Error in retrieving data. Please try again");
}
}
}
Second Fragment (receiving fragment)
public class tt extends Fragment {
TextView txtData;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.ttl, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
txtData = (TextView)view.findViewById(R.id.txtData);
}
public void displayReceivedData(String message) {
txtData.setText("Data received: "+ message);
}
}
Main Activity
public class MainActivity extends AppCompatActivity implements settings.SendMessage {
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
#Override
public void sendData(String message) {
String tag = "android:switcher:" + R.id.view_pager + ":" + 1;
tt f = (tt) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedData(message);
}
}
Something might be breaking in other part of your app.
Below is code of your working example.
class MainActivity
public class MainActivity extends AppCompatActivity implements Setting.SendMessage {
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),this );
ViewPager viewPager = findViewById(R.id.viewpager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.sliding_tabs);
tabs.setupWithViewPager(viewPager);
}
#Override
public void sendData(String message) {
String tag = "android:switcher:" + R.id.viewpager + ":" + 1;
tt f = (tt) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedData(message);
}
}
class Setting
public class Setting extends Fragment {
SendMessage SM;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.setl, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
final EditText inData = (EditText) view.findViewById(R.id.inMessage);
btnPassData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SM.sendData(inData.getText().toString().trim());
}
});
}
public interface SendMessage {
void sendData(String message);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
SM = (SendMessage) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException("Error in retrieving data. Please try again");
}
}
}
class tt
public class tt extends Fragment {
TextView txtData;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.tt, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
txtData = (TextView)view.findViewById(R.id.txtData);
}
public void displayReceivedData(String message) {
txtData.setText("Data received: "+ message);
}
}
class SectionsPagerAdapter
public class SectionsPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2;
private String tabTitles[] = new String[] { "Tab1", "Tab2"};
private Context context;
public SectionsPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
if(position==0){
return new Setting();
}
else
return new tt();
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
I am trying to create a memory game, at the first page when I click on the play button it shows the categories and I used fragments. When I click on a button to go to another fragment it doesn't work.
Mainactivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageViewA = findViewById(R.id.play_title);
imageViewA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
FragmentPlay fragA = new FragmentPlay();
transaction.add(R.id.fragmentContainer_main, fragA);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
}
FragmentPlay.java
public class FragmentPlay extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_play,container,false);
return rootView;
}
public void addFragB(){
FragmentManager childFragMan=getChildFragmentManager();
FragmentTransaction childFragTrans = childFragMan.beginTransaction();
FragmentAnimal fragmentAnimal = new FragmentAnimal();
childFragTrans.add(R.id.fragmentContainer_main2, fragmentAnimal);
childFragTrans.addToBackStack(null);
childFragTrans.commit();
}
}
FragmentAnimal.java
public class FragmentAnimal extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_animal,container,false);
return rootView;
}
}
So I have problem manipulating my TextView inside Fragment.
I have simple callback interface defining only onCallBackReceived(), when my method onCallbackReceived() is called back I want to change text of my TextView, however method setText("onCallback") throws NullPointerException. Exception says that the method is called on null object reference, but I initialized it first using findViewById(). Please help.
public class MyFragment extends Fragment implements MySimpleCallback {
TextView textView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.panel_fragment, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
textView = (TextView) view.findViewById(R.id.text_view);
textView.setText("Okay!");
}
#Override
public void onCallbackReceived() {
textView.setText("onCallback");
}
}
Maybe you can findTheViewById in the onCreateView
public class MyFragment extends Fragment implements MySimpleCallback {
TextView textView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.panel_fragment, container, false);
textView = (TextView) view.findViewById(R.id.text_view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
textView.setText("Okay!");
}
#Override
public void onCallbackReceived() {
textView.setText("onCallback");
}
}
I want the fragment at on Resume stage to go back to on Create View stage if it meet a specific condition, is that possible.
public class FragBeamRec extends Fragment {
public static FragBeamRec newInstance() {
FragBeamRec fragment = new FragBeamRec();
return fragment;
}
public FragBeamRec() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_frag2, container, false);
globalvar.booIstrue = false;
return inflater.inflate(R.layout.fragment_frag2, container, false);
}
#Override
public void onResume(){
super.onResume();
if (globalvar.booIstrue){
// what do i write here to make it back to onCreateView
}
}
}
Just move the onCreateView logic into a separate method.that 's the trick. cheers :)
public class FragBeamRec extends Fragment {
public static FragBeamRec newInstance() {
FragBeamRec fragment = new FragBeamRec();
return fragment;
}
public FragBeamRec() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_frag2, container, false);
doYourMagicHere();
return inflater.inflate(R.layout.fragment_frag2, container, false);
}
#Override
public void onResume() {
super.onResume();
if (globalvar.booIstrue) {
// what do i write here to make it back to onCreateView
doYourMagicHere();
}
}
public void doYourMagicHere() {
globalvar.booIstrue = false;
}
}