4

I am working on the user login procedure

First of all, the user enter a fragment ,

In fragment , it firstly check whether the user has logged in , if true, then inflate the login-ed screen, otherwise, the login form is inflated.

The simple code is like this

if (prefs_user exist) { v = inflater.inflate(R.layout.fragment_logined, container, false); logout.setOnClickListener(new View.OnClickListener() { editor.remove(prefs_user).commit(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.detach(frag).attach(frag).commit(); }); } else { v = inflater.inflate(R.layout.fragment_login, container, false); login.setOnClickListener(new View.OnClickListener() { editor.put(prefs_user, true).commit(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.detach(frag).attach(frag).commit(); }); reg.setOnClickListener(new View.OnClickListener() { intent to start register activity }); } 

Then, for the register activity

if(reg_success) { app.editor.putString("prefs_user",true).commit(); finish(); } 

So far it works for login / logout, the problem is , after register in the new activity, the fragment is not updated e.g. it should show logout screen after the user success registered.

How to refresh the fragment after activity finish? Thanks

1
  • which activity are you finishing Commented Oct 23, 2015 at 4:16

3 Answers 3

11

For solving it you can start register Activity like below :

Intent intent = new Intent(getActivity(), register .class); startActivityForResult(intent, 10001); 

And when you finish your activity you should setResult like below :

setResult(Activity.RESULT_OK) 

Now in your fragment you will get event of finishing your activity by overriding onActivityResult() in the fragment.

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if ((requestCode == 10001) && (resultCode == Activity.RESULT_OK)) // recreate your fragment here FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.detach(frag).attach(frag).commit(); } 

That's it.

Hope it will help you.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect answer. Thanks for your help
1

Your fragment is destroyed when you call finish() on the activity, because your fragment's lifecycle is tied to your activity's lifecycle.

1 Comment

Thanks for reply . the login fragment is stick with main activity and what I am trying to finish is the register activity
1

Considering all android version use this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { fragmentManager.beginTransaction().detach(this).commitNow(); fragmentManager.beginTransaction().attach(this).commitNow(); } else { fragmentManager.beginTransaction().detach(this).attach(this).commit(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.