Reload Fragment after DialogFragment dismisses - android

I have Index.class as the Activity, when the user choose Profile, it will call fragment Profile
if (id == R.id.nav_profile){
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.setCustomAnimations(R.anim.enter,R.anim.exit,R.anim.enter,R.anim.exit);
transaction.replace(R.id.flContent, new Profile(), "ProfileFragment");
transaction.addToBackStack(null);
viewPager.getAdapter().notifyDataSetChanged();
transaction.commit();
}
Now in Profile Fragment, when the user clicks a button it will call DevRegistration Activity
case 1:
btnBeDeveloper.setText("Developer Console");
btnBeDeveloper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent index = new Intent(getActivity(), DevRegistration.class);
startActivity(index);
}
});
break;
Then in DevRegistration class, after I click the register button, it will display a dialog fragment class.
What I want to do is when I click the button inside the dialog fragment, how can I refresh the profile fragment?
Dialog Fragment Class:
public class df_SuccessDevRegistration extends DialogFragment {
Button btnDevGoProfile;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
View rootView = inflater.inflate(R.layout.fragment_success_developer_registration, container, false);
btnDevGoProfile = (Button) rootView.findViewById(R.id.btnDevGoProfile);
btnDevGoProfile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
getFragmentManager().popBackStack();
getActivity().finish();
Profile fragment = (Profile)
getSupportFragmentManager().findFragmentByTag("ProfileFragment");
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
transaction.detach(fragment);
transaction.attach(fragment);
transaction.commit();
}
});
return rootView;
}
}
By the way, I dont know why getSupportFragmentManager isnt working. It shows an error cannot resolve method... when I used getFragmentManager() it crashes.

You could also try this to refresh the current fragment
FragmentTransaction ftran = getActivity().getFragmentManager().beginTransaction();
ftran .detach(this).attach(this).commit();

In your dev registration activity , get your fragment :
Profile profileFragment = getSupportFragmentManager().findFragmentByTag("ProfileFragment");
To refresh it you can try :
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.detach(profileFragment);
transaction.attach(profileFragment);
transaction.commit();
EDIT:
Can you try this ?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
View rootView = inflater.inflate(R.layout.fragment_success_developer_registration, container, false);
btnDevGoProfile = (Button) rootView.findViewById(R.id.btnDevGoProfile);
return rootView;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
btnDevGoProfile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().popBackStack();
Profile fragment = (Profile)
getActivity().getSupportFragmentManager().findFragmentByTag("ProfileFragment");
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction()
transaction.detach(fragment);
transaction.attach(fragment);
transaction.commit();
getActivity().finish();
dismiss();
}
});
}
Hope this helps.

Related

Save and restore fragment state

I have a series of fragments. And I use "previous" and "next" buttons for navigation within this fragments. There are many edit texts and radio buttons in this fragments.
I want to save and and restore the user input in these edit texts and radio buttons when a previous fragment is loaded by clicking on "previous" button.
Screeshots:
Fragment 1
Fragment 2
Fragment 1:
public class Register_Page6 extends Fragment {
public Register_Page6() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_register_page6, container, false);
Button Previous = (Button) view.findViewById(R.id.Previous6);
Button Next = (Button) view.findViewById(R.id.Next6);
Next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.support.v4.app.FragmentTransaction FT;
FT = getActivity().getSupportFragmentManager().beginTransaction();
FT.replace(R.id.main_container,new Register_Page7());
FT.commit();
}
});
Previous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.support.v4.app.FragmentTransaction FT;
FT = getActivity().getSupportFragmentManager().beginTransaction();
FT.replace(R.id.main_container,new Register_Page5());
FT.commit();
}
});
return view;
}
}
Fragment 2:
public class Register_Page7 extends Fragment {
public Register_Page7(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_register_page7, container, false);
Button Previous = (Button) view.findViewById(R.id.Previous7);
Button Regiter = (Button) view.findViewById(R.id.Submit);
Regiter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.support.v4.app.FragmentTransaction FT;
FT = getActivity().getSupportFragmentManager().beginTransaction();
FT.replace(R.id.main_container,new Register_Page6());
FT.commit();
}
});
Previous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.support.v4.app.FragmentTransaction FT;
FT = getActivity().getSupportFragmentManager().beginTransaction();
FT.replace(R.id.main_container,new Register_Page6());
FT.commit();
}
});
return view;
}
}
Add Your fragment to back stack using addToBackStack("YOUTAG") like this in all your fragments :
android.support.v4.app.FragmentTransaction FT;
FT = getActivity().getSupportFragmentManager().beginTransaction();
FT.replace(R.id.main_container, new Register_Page6()).addToBackStack("FragmentName");
FT.commit();
When you have a series of fragments if you add them to the backstack it would be memory wastage.
For this kind of action android provides something called ViewPager, which will always store 2 fragments at a time and create the rest of fragments dynamically when needed.
So if you still decided you don't want to use ViewPager, you can store the Fragment's data in activity's bundle and you can retrieve data when creating the fragment again

popBackStack() not working when going back from Activity 2 Fragment to Activity 1 Fragment

I have 2 Activity - MainActivity and MainActivity2 .
MainActivity has 2 fragments
MainActivity2 has 1 fragment
So i pass data from MainActivity_Frag_one to MainActivity_Frag_two.
When i click device back button and the blue button in MainActivity_Frag_two , it works well by going back to MainActivity_Frag_one.
After that, I pass data from MainActivity_Frag_two to MainActivity2_Frag_one using intent
When i click device back button it works well by going back to MainActivity_Frag_two but the blue button is not working (no effect when clicked, also no error shows).
May i know which part has gone wrong?
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
MainActivity_Frag_one MainActivity_Frag_one=new MainActivity_Frag_one();
transaction.add(R.id.activity_one_container,MainActivity_Frag_one);
transaction.commit();
}
MainActivity_Frag_one.java
public class MainActivity_Frag_one extends Fragment {
EditText get_text;
Button get_button;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_activity__frag_one,container,false);
get_text=(EditText)rootView.findViewById(R.id.input_name);
get_button=(Button)rootView.findViewById(R.id.submit_button);
get_button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
Bundle bundle=new Bundle();
bundle.putString("message", get_text.getText().toString());
MainActivity_Frag_two fragobj=new MainActivity_Frag_two();
fragobj.setArguments(bundle);
transaction.replace(R.id.activity_one_container,fragobj);
transaction.addToBackStack("zzz");
transaction.commit();
}
});
return rootView;
}
}
MainActivity_Frag_two.java
public class MainActivity_Frag_two extends Fragment {
ImageView get_button;
Button get_to_second_activity;
String strtext;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_activity__frag_two,container,false);
TextView display_text=(TextView)rootView.findViewById(R.id.display_text);
strtext=getArguments().getString("message");
display_text.setText(strtext);
get_button=(ImageView)rootView.findViewById(R.id.back_button);
get_button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
getFragmentManager().popBackStack();
}
});
get_to_second_activity=(Button)rootView.findViewById(R.id.to_second_activity);
get_to_second_activity.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(getActivity(),MainActivity2.class);
intent.putExtra("Check","data from mainactivity_frag_two is : "+strtext);
startActivity(intent);
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.addToBackStack("");
transaction.commit();
}
});
return rootView;
}
}
MainActivity2.java
public class MainActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
MainActivity2_Frag_one MainActivity2_Frag_one=new MainActivity2_Frag_one();
transaction.add(R.id.activity_two_container,MainActivity2_Frag_one);
transaction.commit();
}
}
MainActivity2_Frag_one.java
public class MainActivity2_Frag_one extends Fragment {
TextView get_display;
ImageView get_button;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_activity2__frag_one,container,false);
get_display=(TextView)rootView.findViewById(R.id.display);
String data1=getActivity().getIntent().getExtras().getString("Check");
get_display.setText(data1);
get_button=(ImageView)rootView.findViewById(R.id.back_to_previous_frag);
get_button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
getFragmentManager().popBackStack();
}
});
return rootView;
}
}
To go back to the previous activity you don't need to pop the fragment from the back stack. You can just call finish().
In your MainActivity2_Frag_one.java
Replace this :
get_button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
getFragmentManager().popBackStack();
}
});
with
get_button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
getActivity().finish();
}
});
The reason is popBackStack() can be used within the same activity.

Cannot able to open second fragment

On click of Button I am trying to open second fragment(YourResultFragment.java),
I had a lots but don't know why it is not working , here is my code which I am using
public class KnowYourBodyFragment extends Fragment {
public KnowYourBodyFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_know_your_body, container, false);
Button button = (Button)rootView.findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Fragment mFragment = new YourResultFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.know_your_body_container, mFragment ).commit();
}
});
// Inflate the layout for this fragment
return rootView;
}
Use this Try to replace it with Default Container android.R.id.content.
Button button = (Button)rootView.findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment newFragment = new YourResultFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
// Replace whatever is in the fragment_container view with this
// fragment,
// and add the transaction to the back stack
transaction.replace(android.R.id.content, newFragment);
transaction.addToBackStack("tag");
// Commit the transaction
transaction.commitAllowingStateLoss();
}
});
What is transaction.addToBackStack("tag") ?
Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

Go back to previous fragment

Btw this is my second fragment. This will appear once i press a button in a first fragment. In this fragment, if i press the back button on my phone, it will go back to previous fragment. Need your help guys. Thanks in advance.
public class ListAllDoctorFragment extends Fragment {
Button close;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listalldoctor, container,false);
close = (Button) view.findViewById(R.id.button);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.app.FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
SHIT f2 = new SHIT();
ft.add(R.id.content_frame, f2);
ft.addToBackStack(null);
ft.commit();
}
});
return view;
}
}
Assuming you want to go to previous fragment, if that is the case, remove ft.addToBackStack(null); from your code.

How to setOnclick Listener to button in fragment

I have button in fragment1. I want to click this buttton and replace fragment1 by fragment2 but my app still close when I try to run it
This is my code in fragment1
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_menu, container, false);
// gallery = (Gallery) rootView.findViewById(android.R.id.list);
//gallery.setAdapter(new ItemAdapter(generateData(), getActivity()));
Button button = (Button) rootView.findViewById(R.id.imageButtonAll);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, homeFragment);
transaction.commit();
}
});
return rootView;
}
}
You cant open a fragment from inside another fragment.
So you have to move your code from the onClick to your activity and run it there.
In your Activity (lets assume its MainActivity):
public void openMyFragment(){
HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, homeFragment);
transaction.commit();
}
And then, add this in your fragments onClick:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity) getActivity()).openMyFragement();
}
});
The problem is that you are trying to replace a view that does not exist within the view that you are inflating. You have to switch these fragments from your FragmentActivity, which has the contentview containing the layout you are trying to replace.
MainActivity:
class MainActivity extends FragmentActivity {
public Fragment frag1;
public Fragment frag2;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
frag1 = new Frag1();
frag2 = new Frag2();
//assumption to switch to frag 1 immediately
switchToFragment(R.id.frame_container, homeFragment);
...
}
/** Switch UI to the given fragment (example) */
void switchToFragment(Fragment newFrag) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, newFrag)
.commitAllowingStateLoss();
currentFragment = newFrag;
}
}
Fragment:
....
final Activity activity = getActivity();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
about.setTextColor(Color.WHITE);
if(activity instanceof MainActivity) {
((MainActivity) activity).switchToFragment(((MainActivity) activity).frag1));
}
}
});
...

Categories

Resources