reload Activity - Android Activity

Android examples for Activity:Activity Start

Description

reload Activity

Demo Code


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Instrumentation;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.test.InstrumentationTestCase;
import android.test.TouchUtils;
import android.view.KeyEvent;
import android.widget.ListView;
import android.support.v4.app.DialogFragment;

public class Main{
    private static Instrumentation instr;
    public static Activity reloadActivity(Activity activity) {
        int orientation = activity.getResources().getConfiguration().orientation;

        switch (orientation) {
        case Configuration.ORIENTATION_PORTRAIT:
            return forceOrientation(activity,
                    ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        case Configuration.ORIENTATION_LANDSCAPE:
            return forceOrientation(activity,
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        default://from   w  w w  . j a  v a  2  s  . co  m
            throw new RuntimeException("Unknown orientation " + orientation);
        }
    }
    public static Activity forceOrientation(Activity activity,
            int orientation) {
        IntentFilter filter = null;
        Instrumentation.ActivityMonitor monitor = instr.addMonitor(filter,
                null, false);

        activity.setRequestedOrientation(orientation);

        // wait for activity to reload
        Activity new_activity = monitor.waitForActivity();

        instr.waitForIdleSync();

        return new_activity;
    }
}

Related Tutorials