以纵向方式开始活动但是要听取更改

时间:2017-12-24 03:09:39

标签: android android-layout android-studio orientation orientation-changes

我已经做了一些搜索但是,我还没有找到任何问题的答案。这是我的问题:我的活动以纵向显示一些信息。让我们调用此活动A.当用户将其设备旋转到横向时,将启动新活动,活动B.与活动A类似,如果活动B旋转为纵向,则活动A启动。这就是问题所在。如果用户的设备在首次启动应用程序之前已处于格局中,则活动A将以横向加载(无bueno)。

在活动A中,我在清单中尝试了setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);以及android:screenOrientation="portrait",但是,如果我使用其中任何一种方法,请覆盖onConfigurationChanged(Configuration newConfig)来监听改变不再有效。

TDLR;

有没有办法保证我的应用程序在纵向加载,同时仍在听取方向更改?如果那不可能,是否有任何其他方法或解决方案?

相关(足够)代码:

MainActivity.java (活动想以肖像方式开始,A.K.A,活动A)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fabric.with(this, new Crashlytics());

    // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    // Adding before setContView() doesn't work
    setContentView(R.layout.activity_main);
    // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    // Adding after setContView() doesn't work
    // anywhere I add sRO() in onCreate, overriding onConfiurationChange
    // won't register a change

    fab = findViewById(R.id.fab);

    showTutorial();

    PreferenceManager.setDefaultValues(this, R.xml.pref_main, false);
    preferences = PreferenceManager.getDefaultSharedPreferences(this);
    numberOfUnlocksPref = getSharedPreferences(getString(R.string.key_num_unlocks), MODE_PRIVATE);
    numberOfUnlocks = numberOfUnlocksPref.getInt(getString(R.string.key_num_unlocks), 0);

    IntentFilter updateUi = new IntentFilter();
    updateUi.addAction(ButtonReceiver.BROADCAST_LOCKS_RESET); // Listen for a reset
    updateUi.addAction(LockService.UPDATE_UI); // Makes sure the UI updates on device lock/unlock

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // If I don't use sRO() anywhere in the Main, this code works
        // BUT, the app can still start in landscape (no bueno)
        // If i do use sRO() this code won't work
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            startActivity(new Intent(this, LockDataActivity.class));
        }
    }
}

LockDataActivity.java (我希望从横向开始的课程,这项工作一切顺利)

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_data);
    if (getSupportActionBar() != null) {
        getSupportActionBar().hide();
    }

}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        finish();
    }
}

@Override
public void onBackPressed() {
    Configuration configuration = new Configuration();
    if (configuration.orientation != Configuration.ORIENTATION_PORTRAIT) {
        Toasty.error(this, "Please rotate your device to go back", Toast.LENGTH_SHORT).show();
    }
}
}

的Manifest.xml

 <activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize">

如果我添加android:orientation="portrait",那么我会覆盖onConfigChange不起作用

0 个答案:

没有答案
相关问题