initialize Activity using data from SharedPreferences - Android Activity

Android examples for Activity:Activity Feature

Description

initialize Activity using data from SharedPreferences

Demo Code


//package com.java2s;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.preference.PreferenceManager;

public class Main {
    public static void initialize(Activity activity) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(activity);

        String orientation = prefs.getString("prefOrientation", "Null");
        if ("Landscape".equals(orientation)) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else if ("Portrait".equals(orientation)) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {//from w  w  w  .  j a va2s  .  c o  m
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
        }
    }
}

Related Tutorials