1

I'm developing an app which counts number of times the app goes to the background. It also retains the values when the orientation is changed. Though I have it working of all use cases, I have one use case which does not work.

Case : When I press the home button, change the phone's orientation and reopen the app, It does open in landscape mode but, the background count does not increase. I have tried setting values in all the life cycle methods. It doesn't work. Hope somebody can help me with this. `

    public class MainActivity extends AppCompatActivity {

    private int clickCount =0, backgroundCount = 0;
    TextView tvClickCountValue, tvBackgroundCountValue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if( savedInstanceState != null){
            clickCount = savedInstanceState.getInt("COUNT");
            backgroundCount = savedInstanceState.getInt("BGCOUNT");
        }
        setContentView(R.layout.activity_main);
        tvClickCountValue = (TextView) this.findViewById(R.id.tvClickCountValue);
        tvBackgroundCountValue = (TextView) this.findViewById(R.id.tvBackgroundCountValue);
        setView(MainActivity.this);

    }

    public void onClick(View v){

        clickCount += 1;
        tvClickCountValue.setText(Integer.toString(clickCount));

    }

    public void setView(Context ctx){

        tvClickCountValue.setText(Integer.toString(clickCount));
        tvBackgroundCountValue.setText(Integer.toString(backgroundCount));
    }

    @Override
    protected void onStop() {
        super.onStop();
        backgroundCount += 1;

    }


    @Override
    protected void onResume() {
        super.onResume();
        tvClickCountValue.setText(Integer.toString(clickCount));
        tvBackgroundCountValue.setText(Integer.toString(backgroundCount));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("COUNT", clickCount);
        outState.putInt("BGCOUNT", backgroundCount);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        clickCount = savedInstanceState.getInt("COUNT");
        backgroundCount = savedInstanceState.getInt("BGCOUNT");
    }
}
Charuක
  • 12,953
  • 5
  • 50
  • 88
Pavan
  • 79
  • 11
  • `onPause()` method is called when app is going to background or being closed (or killed), increment counter in `onPause()` and decrement in `onDestroy` to take into account closing. – Arman P. Jan 28 '17 at 01:06
  • This is interesting article: http://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo?rq=1 – Stanojkovic Jan 28 '17 at 01:09
  • You can use Sharedpreference. – Mohammad Tauqir Jan 28 '17 at 01:10

2 Answers2

1

This article contains useful information: https://developer.android.com/guide/topics/resources/runtime-changes.html especially in the section about Handling The Change.

Try handling it on onConfigurationChanged() of you have disabled Activity restarts on orientation changes. Otherwise, probably through this article you will find which case applies to your scenario.

By reading the problem description, I am assuming that if you don't rotate the device the application works as intended.

Iakovos
  • 1,842
  • 4
  • 25
  • 30
  • That's correct. If I don't rotate the device, the background count works fine. The problem occurs only when I move the app to the background, rotate the home screen and pull the application back to the foreground. It stays the same. Ideally, It should increment the counter. – Pavan Jan 28 '17 at 02:48
  • 1
    My guess is what I mentioned above. Did you have any luck with that? Play around with adding/removing `android:configChanges="orientation|keyboardHidden"` in your Activity declaration in the Manifest file and put breakpoints to lifecycle methods to see where it goes when you resume. `onConfigurationChanged()` is a good bet, and it was not in your code. – Iakovos Jan 28 '17 at 10:40
0

you need to persist the count in the SharedPreferences. each time you reopen the app read the last value from the SharedPreferences. And increment and save to SharedPreferences each time the app is hidden.

Then you can count the with @kiran code:

public class BaseActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    public static boolean isAppInFg = false;
    public static boolean isScrInFg = false;
    public static boolean isChangeScrFg = false;

    @Override
    protected void onStart() {
        if (!isAppInFg) {
            isAppInFg = true;
            isChangeScrFg = false;
            onAppStart();
        }
        else {
            isChangeScrFg = true;
        }
        isScrInFg = true;

        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (!isScrInFg || !isChangeScrFg) {
            isAppInFg = false;
            onAppPause();
        }
        isScrInFg = false;
    }

    public void onAppStart() {
      // app in the foreground
      // show the count here.

    }

    public void onAppPause() {
      // app in the background
      // start counting here.
    }
}
Community
  • 1
  • 1
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • Well, I'm saving the instance and restoring it. So, it works fine when I don't change the orientation. But If I rotate the device, the values persist but, It does not increment the counter. – Pavan Jan 28 '17 at 02:51