1

I am creating a login page which will have forgot password right now:

  1. Login Page is one Activity and Forgot Password page is another Activity.
  2. Each one calls an AsyncTask for http post which returns a json response.
  3. Hence I have callback listener in each `Activity.
  4. In the call back listener I get the json response, which I parse in separate Activity for each Login and separate one for Forgot password

I need to know whether its good practice to do this way or use fragments. Also any other better way to developing it?

mmBs
  • 8,421
  • 6
  • 38
  • 46
Sweety Bertilla
  • 972
  • 10
  • 35

1 Answers1

4

I would suggest using a Fragment per page, so one for "Login" and one for "Forgot Password", and one Activity that handles the async http request. You would then be able to use the fragment methods such as fragment.replace() to swap out the screens without each fragment having to have a callback listener. The activity then can also parse the callback listener once instead of twice in each separate activity. Also, the way Android is heading, Fragments are better to use for UI elements than separate Activities as it makes UI updates quicker and smoother then starting a new activity.

There is an Android Developer API Guide loaders available to every Activity and Fragment. Why not check it out. On that same site you can also find guides to use fragments in your activity.

Adam
  • 2,532
  • 1
  • 24
  • 34
  • if I implement fragments, after a button click in fragment how will I let the Activity know that that action is taken place and the activity needs to call the http async task? – Sweety Bertilla Feb 25 '14 at 19:38
  • You could do this with a public method inside the activity called by your `onClick()` method of the fragment controlling the buttons in the fragment's layout. There are other ways, but this appears easiest to me. – Adam Feb 25 '14 at 19:46
  • @Sweety check out this question. http://stackoverflow.com/questions/12659747/call-an-activity-method-from-a-fragment – Adam Feb 25 '14 at 19:52