我的Android应用程序遇到了非常令人不安的麻烦。我正在使用Fragments
。每个片段都包含一个用于Portrait UI和Landscape UI的单独XML文件。
如果我从片段“InputFragment
”移动到片段“OutputFragment
”,如果我现在旋转设备,片段会变为“InputFragment
”!!这意味着,应用程序将在轮换时返回到上一个片段!但是,这不是应该发生的事情。如果我在设备处于纵向状态时旋转设备,那么它应该调用相同片段的横向UI,反之亦然。
以下是代码。我只显示代码的结构,因为所有其他的都是变量声明和赋值等。
InputFragment.java
public class InputFragment extends Fragment {
//Declaring variables. CODE REMOVED
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Intializing instance variables. CODE REMOVED
view = inflater.inflate(R.layout.input, container,false);
restoreStates(savedInstanceState);
return view;
}
//This method will save the instances
@Override
public void onSaveInstanceState(Bundle savedInstanceStateBundle)
{
super.onSaveInstanceState(savedInstanceStateBundle);
try{
}
catch(NullPointerException e)
{
}
}
//This method will restore the instances
@Override
public void onActivityCreated(Bundle savedInstanceStateBundle)
{
super.onActivityCreated(savedInstanceStateBundle);
if(savedInstanceStateBundle!=null)
{
}
}
//Action Listener for the Submit Bar
private class SubmitButtonAction implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
//Put the JSON. Calling to the JSON handling class. CODE REMOVED
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Fragment frag = new OutputFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, frag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
}
private void restoreStates(Bundle savedInstanceStateBundle)
{
if(savedInstanceStateBundle!=null)
{
}
}
}
OutputFragment.Java
public class OutputFragment extends Fragment implements AsyncResponse{
//Delclaring variables. CODE REMOVED
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Intializing instance variables. CODE REMOVED
view = inflater.inflate(R.layout.output, container,false);
return view;
}
//ActionListener for the manual back button
private class BackButtonAction implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
fm.popBackStackImmediate();
}
}
@Override
public void processFinish(String output) {
// TODO Auto-generated method stub
Log.d("OUTPUT_FRAGMENT", output);
try {
progressDialog.dismiss();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.Window;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Fragment frag = new InputFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, frag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.xxx"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/xxx_logo"
android:label="@string/xxx"
android:theme="@style/AppTheme">
<activity
android:name="com.xxx.xxx.MainActivity"
android:configChanges="keyboardHidden|screenLayout|orientation|screenSize"
android:label="@string/xxx" >
</activity>
<activity
android:name="com.xxx.xxx.SplashScreen"
android:configChanges="keyboardHidden|screenLayout|orientation|screenSize"
android:label="@string/xxx" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
搜索了几个小时后,我找到了解决方案来解决这个问题。也就是说,将代码android:configChanges="keyboardHidden|screenLayout|orientation|screenSize"
添加到清单文件中。
现在,Fragment不会移动到Rotation上的前一个片段,但是,它也不会加载Other布局!这意味着,如果我将手机从纵向旋转到横向,它不会加载“横向”用户界面,反之亦然!它只是试图将自己调整到视图并且它不好,因为在不加载其他相关UI的情况下,图像和所有内容都会显示得很糟糕。这里发生了什么?
答案 0 :(得分:1)
旋转后,您的Activity将被重新创建并调用其onCreate()
方法。
你想要做的是检查活动是否是第一次启动,然后用InputFragment
替换你的片段。在Activity第一次创建savedInstanceState为null,所以你可以这样做:
if(savedInstanceState == null) {
// replace fragment
}
修改强>
这就是您的onCreate()
活动应该如何显示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
if(savedInstanceState == null) {
Fragment frag = new InputFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, frag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
}
现在,在方向更改活动中不会替换您的片段,您不需要在Manifest中设置任何attrs