我有一个带有双窗格的活动:左侧的菜单项列表(片段M)和右侧当前所选项目的详细信息(片段D).
当用户选择片段D中的项目时,片段D将被另一个片段替换(让我们称之为D1).当用户选择片段D1中的项目时,片段D1将被另一个片段替换(让我们称之为D2),依此类推.当然,当用户按下后退按钮时,她可以返回片段历史记录:D2-> D1-> D.
我的问题是:如何在配置更改时保存和恢复完整的片段后备栈(特别是在屏幕方向上)?
我之前没有其他人遇到这个问题似乎很奇怪,但我在Google和Stack Overflow上的搜索过程中没有发现任何相关信息.如果我错过任何相关的帖子,请告诉我.
我知道我可以简单地将android:configChanges="orientation|screenSize"属性添加到我的活动中以避免活动娱乐,但我根本无法做到这一点.
我不能这样做的原因是我使用Action Bar Sherlock(第4节)来实现向后兼容性,并且该组件需要活动重新创建才能在配置更改时正常运行,AFAIK.
如果有其他方法重新创建Action Bar Sherlock组件而不破坏并重新创建活动,请告诉我.
提前致谢.
我想知道 Android 是否有任何标志要添加到configChangesAndroidManifest 的 Activity 属性中,以便在设备的“反转颜色”选项中进行修改。
在Android的文档显示以下标志: - “MCC” 
- “跨国公司” 
- “区域设置” 
- “触摸屏” 
- “键盘” 
- “keyboardHidden” 
- “导航” 
- “屏幕布置” 
- “fontScale” 
- “uiMode” //这是暗模式
- “方向” 
- “密度” 
- “screenSize” 
- “smallestScreenSize”  
但他们都没有处理它。
反转颜色选项:
我有一个小活动,包含EditText和imageview以及一个按钮.当您按下按钮时,它会启动相机以获得结果,当它返回时,它会将图像视图更改为您刚刚拍摄的图像.
但是当方向改变时,imageview将重置为布局上的默认值.
我尝试做的是设置一个名为custom的布尔值,当你拍照时,它将其设置为true.我覆盖onConfigurationChanged(),如果custom设置为true,我恢复图像.
我现在的问题是EditText被删除 - 如何在配置更改后恢复EditText?我的第一次尝试是将它的内容存储到String onPause()中,然后将其恢复,但它总是空白.
android onconfigurationchanged android-layout android-edittext android-orientation
屏幕旋转后,其中一个视图对象的高度发生变化,我想知道像素中的高度值.
该onConfigurationChanged方法可能在视图完成旋转之前调用.因此,如果我在此方法中测量视图大小,则大小仍然是之前的旋转值.
问题是如何在旋转完成后获取视图大小值而不重建Activity.
我目前在我的Android应用程序中有一个选项卡屏幕,它从其选项卡中托管其他4个活动.
我按如下方式设置每个标签:
intent = new Intent().setClass(this, LiveActivity.class);
spec = tabHost.newTabSpec("live_tab").setIndicator(getString(R.string.tab_contacts),
res.getDrawable(R.drawable.ic_tab_contacts)).setContent(intent);
tabHost.addTab(spec);
一切正常,但是当我在设备上更改方向时,我会在每个单独的活动中单独处理方向更改,每个活动在XML中也有不同的横向布局.
当我更改设备上的方向时,对于我当前正在进行的活动,更改可以正常工作,但如果我将方向改回(因此将其纵向更改为横向到纵向),那么我的活动似乎会重新加载并重置为其初始状态而非而不是尊重我的onRetainNonConfigurationInstance()方法.
有没有人遇到过这个问题,也许会遇到一个解决方案?
我有一个应用程序(这里),其中有Admob横幅,我不喜欢每次更改方向时都会重新加载横幅的事实,所以很长一段时间我都有相同的解决方法,以避免重新创建活动,因此避免重新创建Admob视图:
<activity ... android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:screenOrientation="fullUser" ...>
这很好用,但它不是一个好的解决方案或推荐.只是因为这个问题我必须使用的解决方法(在这里写了一下).
问题是某些UI元素(例如工具栏)不会根据新方向进行更改.
这会导致工具栏保持与应用程序启动时相同的高度和相同的字体大小的行为.
肖像 - >风景示例:
正如你所看到的,景观的高度保持很大,字体大小也很大.
横向 - >肖像示例:
在这里你看到相反的情况.高度保持很小,字体大小也很小.
布局基本上是这样的:
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/activity_app_list__drawerLayout" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent" android:layout_height="match_parent"
            android:orientation="vertical">
            <FrameLayout
                android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary">
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/activity_main__toolbar" android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize" android:background="@null"
                    android:theme="?attr/actionBarTheme" tools:ignore="UnusedAttribute"/>
            </FrameLayout>
            <RelativeLayout
                android:layout_width="match_parent" android:layout_height="match_parent">
                <FrameLayout
                    android:id="@+id/activity_app_list__fragmentContainer"
                    android:layout_width="match_parent" android:layout_height="match_parent"
                    android:layout_above="@+id/activity_app_list__adContainer"/>
                <ImageView
                  android:layout_width="match_parent" android:layout_height="wrap_content"
                    android:src="@drawable/msl__action_bar_shadow"
                    tools:ignore="ContentDescription"/>
                <FrameLayout
                    android:id="@+id/activity_app_list__adContainer"
                    android:layout_width="match_parent" android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"/>
            </RelativeLayout>
        </LinearLayout>
        <ScrollView
            android:id="@+id/activity_app_list__drawerView" android:layout_width="240dp"
            android:layout_height="match_parent" android:layout_gravity="start"
            android:background="?android:attr/windowBackground">
            <LinearLayout
                android:layout_width="match_parent" android:layout_height="wrap_content" …我想做一个允许在某些条件下改变方向的活动,但不是.更确切地说,我想阻止后台线程忙时重新启动活动.
我已将该configChanges属性放在活动清单上,并onConfigurationChanged在方向更改时调用.但是,我想允许应用程序在允许时更改方向.
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (orientationChangeAllowed) {
        // how do I restart this activity?
    } else {
        // don't do anything
    }
}
我需要为我的活动的纵向和横向方向应用不同的布局.此外,如果方向是肖像,我需要显示警报.
我android:configChanges="orientation|keyboardHidden"在AndroidManifest中指定了.我也像这样重写onConfigurationChanged方法:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
    Log.d("tag", "config changed");
    super.onConfigurationChanged(newConfig);
    int orientation = newConfig.orientation;
    if (orientation == Configuration.ORIENTATION_PORTRAIT)
        Log.d("tag", "Portrait");
    else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
        Log.d("tag", "Landscape");
    else
        Log.w("tag", "other: " + orientation);
    ....
}
从横向旋转到纵向日志时看起来像:
config changed
Portrait
但是,当从纵向变为风景时,它看起来像
config changed
Portrait
config changed
Landscape
为什么onConfigurationChanged被调用两次?我怎么能避免呢?
我需要获取WebView的宽度和高度,并将它们传递到要在WebView中加载的URL的查询字符串中。我在onResume()中找到了一种方法。由于此时尚未计算宽度和高度,因此我在UI加载后将Runnable发布到要排队的WebView中。
这是我的问题:当方向更改时,我在onConfigurationChanged中处理它。当我尝试在onConfigurationChanged中将Runnable发布到WebView时,WebView的宽度和高度最终是旧方向的宽度和高度。方向更改后,在什么时候可以截取新的宽度和高度?
android width orientation screen-orientation onconfigurationchanged
我正在使用 Android 导航组件,如果我在第二个片段中,我有一个包含三个片段的活动,并旋转屏幕强制活动重新启动导航返回到开始目的地。
当活动重新启动时,navhostFragment 不应该保留图形的状态吗?
或者这里发生了什么默认行为?
即使添加“有效”,我也不想添加以下内容
 android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
因为不想自己处理方向变化,希望系统正常处理,还保留导航状态
如果有帮助,我会提供一些我的代码
在活动中我使用 navController.setGraph() 所以我可以像这样将数据传递到开始目的地
 navController = Navigation.findNavController(
        this,
        R.id.nav_host_fragment
    )
 setSupportActionBar(findViewById(R.id.toolbar))
 appBarConfiguration = AppBarConfiguration.Builder(navController.graph).build()
 supportActionBar?.setDisplayHomeAsUpEnabled(true)
 intent.putExtra("EXTRA_KEY","some_data")
 navController.setGraph(R.navigation.nav_graph,intent.extras)
我使用这个从片段导航到片段
navController.navigate(FirstFragmentDirections.actionFirstFragmentToSecondFragment())
这是 nav_graph 中的代码
<fragment
    android:id="@+id/FirstFragment"
    android:name="com.example.app.presentation.ui.FirstFragment"
    android:label="FirstFragment" >
    <action
        android:id="@+id/action_FirstFragment_to_secondFragment"
        app:destination="@id/secondFragment"
        app:enterAnim="@anim/enter_from_right"
        app:exitAnim="@anim/exit_to_left"
        app:popEnterAnim="@anim/enter_from_left"
        app:popExitAnim="@anim/exit_to_right"
        />
</fragment>
<fragment
    android:id="@+id/secondFragment"
    android:name="com.example.app.presentation.ui.secondFragment"
    android:label="secondFragment"
    tools:layout="@layout/fragment_second" />
感谢您的帮助
android onconfigurationchanged android-architecture-components android-jetpack android-architecture-navigation
android ×10
orientation ×3
android-architecture-components ×1
android-architecture-navigation ×1
back-stack ×1
java ×1
restart ×1
tabactivity ×1
width ×1