Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh(recreate) the activities in back stack when change locale at run time

I have an Activity say ActivityMain from this activity I moved to another activity called ActivitySettings and in settings activity I'm changing the App locale by clicking on a button, and using recreate I achieved the change I need in current activity but when I press back my `ActivityMain' will resume but locale is not updated.

Can some one tell me how to 'Recreate' backstack activities? what will be the correct approach.

I can't call recreate on refresh as it will be infinite loop

like image 243
Praneeth Avatar asked Jun 29 '18 08:06

Praneeth


People also ask

What happens to the back stack when you switch between activities?

If the user continues to press or gesture Back, then each activity in the stack is popped off to reveal the previous one, until the user returns to the Home screen (or to whichever activity was running when the task began). When all activities are removed from the stack, the task no longer exists.

What is taskaffinity in Android?

Task affinity lets you define which task an activity belongs to. By default, an activity has the same task affinity as its root activity. With task affinity, we can now separate activities into different tasks.

How do I close all activities on Android?

exit(); or finish(); it exit full application or all activity.


1 Answers

In each Activity's onCreate() you can maintain the currentLangCode. Check this value in onResume(), if it differs, you can conclude the locale was change and recreate()

You can do it as follows:

public class ActivityA extends AppCompatActivity{
    private String currentLangCode;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        currentLangCode = getResources().getConfiguration().locale.getLanguage();
        ...
    }
    @Override
    public void onResume(){
        ...
        if(!currentLangCode.equals(getResources().getConfiguration().locale.getLanguage())){
            currentLangCode = getResources().getConfiguration().locale.getLanguage();
            recreate();
        }
    }
    ...
}

My Recommendation

If you want to apply it for all the Activities, then simply create BaseActivity as follows:

public class BaseActivity extends AppCompatActivity{
    private String currentLangCode;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        currentLangCode = getResources().getConfiguration().locale.getLanguage();
        ...
    }
    @Override
    public void onResume(){
        ...
        if(!currentLangCode.equals(getResources().getConfiguration().locale.getLanguage();)){
            currentLangCode = getResources().getConfiguration().locale.getLanguage();
            recreate();
        }
    }
    ...
}

Extend all Activities from BaseActivity

public class ActivityA extends BaseActivity{

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }
    @Override
    public void onResume(){
      super.onResume();
    }
    ...
}
like image 195
Sagar Avatar answered Oct 26 '22 23:10

Sagar