0

在网上长时间搜索后仍然无法解决我的问题。

显现:

<application
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Application"
        android:configChanges="keyboardHidden|orientation"
        android:label="@string/title_activity_application" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

应用布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/white" >
        <WebView
            android:id="@+id/navWeb"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/mainFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="be.smartschool.mobile.MainFragment" ></fragment>

    </LinearLayout>

片段布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView 
    android:id="@+id/mainWeb"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

应用活动:

public class Application extends Activity {
protected WebView mainWeb;
protected FrameLayout mainWebPlaceholder;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_application);
    //mainWeb = (WebView) getLastNonConfigurationInstance();
    //initUI();
}

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

片段类

public class MainFragment extends Fragment {
protected WebView mainWeb;

public MainFragment(){
    this.setRetainInstance(true);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_mainweb, container, false);
    Log.i("INFO", ">>>>>>> onCreateView");
    if(mainWeb != null) {
        mainWeb.restoreState(savedInstanceState);
        Log.i("INFO", ">>>>>>> restoreState");
     }
    return view;
}

 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
     super.onActivityCreated(savedInstanceState);
     if(mainWeb == null) {
         initUI();
     }
 }

 @Override
 public void onSaveInstanceState(Bundle outState) { 
     if(mainWeb != null) {
         Log.i("INFO", ">>>>>>> onSaveInstanceState");
         mainWeb.saveState(outState);
     }
 }

protected void initUI(){
    Log.i("INFO", ">>>>>>> UI Init called");

    // Create the webview
    mainWeb = (WebView) getActivity().findViewById(R.id.mainWeb);
    if(mainWeb != null) {
        mainWeb.getSettings().setSupportZoom(false);
        mainWeb.getSettings().setBuiltInZoomControls(false);
        mainWeb.getSettings().setLoadsImagesAutomatically(true);
        mainWeb.getSettings().setJavaScriptEnabled(true);
        // Load a page
        mainWeb.loadUrl("http://www.google.com");

        Log.i("INFO", ">>>>>>> WEBVIEW CREATED");
    }
}
}

所以你可以看到我使片段可保留,并且当我调试日志时是逻辑的。webview 创建一次,但仅在第一次可见。在orientationchange之后,我再也看不到webview了。Whet 在这里绞尽脑汁。在我看来这是一个小问题,但我找不到合适的解决方案

4

2 回答 2

0

您可能需要覆盖 onConfigurationChanged。

于 2012-09-06T13:24:52.887 回答
0

尝试android:configChanges="orientation"在您的 AndroidManifest.xml 中放入您的活动标签

希望它会工作......

于 2012-09-06T13:25:39.630 回答