0

I have 3 pages in my application. Page A,B,C. When i click next button in A page the page will navigate to B page. In that page there will be some request and response process and Progress indicator will happen. Then when i click next the page will navigate to C page. There also some request and response process and Progress indicator process will happen. Now my problem is when i click back button from page C The page is navigate to B page. But the request response process and progress indicator process is working. Here i don't want do this process when i click back button. Now i have try like this:-

@Override
protected void onStart() {
    super.onStart();
    Log.v(this.getClass().toString(),"onStart");
}

Here the request and response process is not working when i click back button. But the progress indicator is loading. This progress indicator is continuously rolling. How to disable all the functions. I just want to go back. Do not do any other work. Please help me to solve this issue. Sorry For the poor English..

4
  • How do you navigate from one page to another? The pages are activities? Jul 16, 2014 at 7:37
  • Just using Intent.. protected String doInBackground(String... params) { Intent intent = new Intent(Class_B.this, Class_A.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); return "Success"; }
    – Vijay
    Jul 16, 2014 at 8:47
  • 1
    As you can see, you add a flag Intent.FLAG_ACTIVITY_CLEAR_TOP . This will clear all previous activities. And also you finish the current activity, so pressing back will call onCreate where you have those requests and process stuff. Avoid adding flag and finish() method. Jul 16, 2014 at 9:11
  • yes..Thanks for the idea..!!! And also i did another mistake also. In the previous class i did not dismiss the progress indicator. Thanks for your idea..
    – Vijay
    Jul 16, 2014 at 10:01

5 Answers 5

1

As you can see, you add a flag Intent.FLAG_ACTIVITY_CLEAR_TOP. This flag will clear all previous activities.

Intent.FLAG_ACTIVITY_CLEAR_TOP - If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

And also you finish the current activity, so navigating back to page B call B's onCreate method where you have those requests and process stuff. Avoid adding flag and finish() methods.

1

You don't want to override the onCreate() method for this usecase, why would you do that? The activity is created, and the process will run. What you want is to override the onBackPressed() method, so that you cannot exit the Activity until the process is complete.

Similarly to these: Android: Proper Way to use onBackPressed() with Toast but here you want to make a boolean that is set to false while the process is not complete, and set to true when it's done. Allow onBackPressed() to call super.onBackPressed() only when the boolean is true.

@Override
public void onBackPressed()
{
    if(processesCompleted == true)
    {
        super.onBackPressed();
    }
}
0

You can't disable the onCreate() method in android but you can however override it like you do with the onStart().

I suggest you take a look at the life cycles of either fragments or activities, depending on which you use.

Fragments: http://developer.android.com/guide/components/fragments.html

Activities: http://developer.android.com/reference/android/app/Activity.html

It is hard to understand what the problem really is without more code, but basically the code in onCreate will mostly not be called when you press the backbutton since an activity will be paused until the phone needs more resources and then destroys it. If an activity is destroyed the onCreate method will be called and there is nothing you can do to change that.

If my answer dosen't help, please provide more example code.

0

If all your cards have different activities, the backbutton should work perfectly. However if your activity is brought to the front, nothing will be reloaded, except the onresume. In the onresume you can perform a new loading structure or something you want to achieve.

When you don't have different activities, use the override at onBackPressed(), that will handle the backbutton.

But place some code for a better answer

0

You can create a new function in which you run the processes and call it on onCreate and use a Boolean flag to make sure when you go back to that activity and flag is checked the function is not called. Then save the value of flag in savedPreference onPause() method and you are done and onCreate() method load your saved preference.

Something like this

boolean flag = false;

@Override
public void onResume()
{
   super.onResume();
   // load savedPreferences
}

@Override
public void onPause()
{
   super.onPause();
   //save savedPreferences
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    if(flag == false)
    {
        //function call
        processes();
    }
}

private void processes()
{
    flag = true;
    // do stuff here
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.