Question

I have this Fragment Activity which is my main activity and include 2 Fragment Activity.

    /** SharedPreferences */
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedpreferences;
int currentOrientation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

    /** Rotation SharedPreferences */
    SharedPreferences preferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    int orienation = preferences.getInt("orientation", -1);
    if (orienation != -1) {
        setRequestedOrientation(orienation);
    }
    /** End */

    mAdapter = new FragmentAdapter(getSupportFragmentManager());

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);

}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (outState == null) {
        outState = new Bundle();
    }
    outState.putInt("currentOrientation",
            Integer.valueOf(currentOrientation));
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    currentOrientation = savedInstanceState.getInt("currentOrientation");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menus, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        /** Rotation */
    case R.id.menuRotate:
        SharedPreferences preferences = PreferenceManager
        .getDefaultSharedPreferences(this);
        Editor editor = preferences.edit();
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            editor.putInt("orientation",
                    ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            editor.putInt("orientation",
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        editor.commit();

        break;

    }
    return false;
}

}

I had set the orientation option in the menu.

The user can change the orientation from the menu to be in landscape or portrait.

Its working nice, BUT the problem is the other activities.

How can I read from the shared-preferences that I'v already declared in my main activity

what I want is that all activities in my app get the same orientation what the users choice from the menu.

This is one of my other activity:

public class MainList extends Activity {

LinearLayout b1;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_list);
    b1 = (LinearLayout) findViewById(R.id.list_a);
    b1.setOnClickListener(mb1);

}

View.OnClickListener mb1 = new View.OnClickListener() {
    public void onClick(View v) {
        startActivity(new Intent(MainList.this, List_1.class));
    }
};

Thanks in Advance :)

Was it helpful?

Solution

So, in each you should be using the same context and not just "this".

private Context context; 

public void onCreate(){ 
     super.onCreate();
     context = getApplicationContext();
}

public static Context getAppContext() {
       return context;
}

You can make this context accessible by other classes that do not have a type (just function holders).

Then, in your use of Shared Prefs :

final SharedPreferences prefList = context.getSharedPreferences(
                Constants.PREFERENCE_NAME, Context.MODE_PRIVATE);

If everyone is using getApplicationContext() then they will be able to talk to one another as they are all using the same Context. I would always suggest that you use Context.MODE_PRIVATE unless you need external applications to be able to grab information from those SharedPreferences.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top