序言:我使用的是ActionBarSherlock,目标SDK是17(Android 4.2),Min SDK是5(Android 2.0)
情况:
我的应用中有一个片段,它提供了某种登录方式。它允许用户在设备上拥有多个用户帐户(因为Android< 4.2没有系统支持,我们的用户似乎因设备共享而使用它。)
片段布局包含列表视图,大型横向图片也包含添加新帐户的按钮(否则可通过溢出菜单)。
listview附加到名为" AccountAdapter"的BaseAdapter-Derivate,它从数据库获取帐户并通过layoutinflater为每个帐户创建相应的子视图。有3种可能性:用户提供登录和密码/用户仅提供登录/用户提供的任何内容。因此,对于这些情况有3种不同的布局,其中通过EditText-Elements请求丢失的数据,并且通过TextView显示存储的数据。此外,缺少数据的布局提供了用于存储缺失数据和提交按钮的复选框。如果提供了所有数据,则缺少提交按钮的onclicklistener将直接附加到帐户根视图。 onclicklistener将输入的数据发送到网络代码类并修改适配器模式;在此之后,适配器仅显示所选条目,但使用"进度"查看,以便向用户显示当前操作("用户XXX正在登录,请等待......")。
这已经很好了。
问题:
在方向更改时,所有输入的数据都会丢失。输入登录数据,密码,有关是否应存储登录数据的信息。
布局:
<!-- layout/main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="@id/main_fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- layout-large-land/main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="@id/main_fragment_sidebar"
android:layout_width="@dimen/main_sidebar_width"
android:layout_height="match_parent" />
<FrameLayout
android:id="@id/main_fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- layout/fragment_login.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@id/fragment_login_accountlist"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
<!-- layout-large-land/fragment_login.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@id/fragment_login_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/string_fragment_login" />
<ListView
android:id="@id/fragment_login_accountlist"
android:layout_width="@dimen/fragment_login_accountlist_width"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
<!-- layout/view_login_account_new.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@id/view_login_account_profileimage"
android:layout_width="@dimen/view_login_account_profileimage_width"
android:layout_height="@dimen/view_login_account_profileimage_height"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/string_view_login_account_profileimage_contentdescription"
android:scaleType="fitCenter"
android:src="@drawable/img_kb" />
<EditText
android:id="@id/view_login_account_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/view_login_account_profileimage"
android:ems="10"
android:hint="@string/string_view_login_account_username_hint"
android:inputType="text" >
<requestFocus />
</EditText>
<EditText
android:id="@id/view_login_account_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/view_login_account_username"
android:layout_alignParentRight="true"
android:layout_below="@id/view_login_account_username"
android:ems="10"
android:hint="@string/string_view_login_account_password_hint"
android:inputType="textPassword" />
<CheckBox
android:id="@id/view_login_account_storecredentials"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/view_login_account_password"
android:layout_toRightOf="@id/view_login_account_profileimage"
android:text="@string/string_view_login_account_storecredentials_text" />
<Button
android:id="@id/view_login_account_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/view_login_account_storecredentials"
android:text="@string/string_view_login_account_submit_text" />
</RelativeLayout>
<!-- layout/view_login_account_progress.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@id/view_login_account_profileimage"
android:layout_width="@dimen/view_login_account_profileimage_width"
android:layout_height="@dimen/view_login_account_profileimage_height"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/string_view_login_account_profileimage_contentdescription"
android:scaleType="fitCenter"
android:src="@drawable/img_kb" />
<TextView
android:id="@id/view_login_account_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/view_login_account_profileimage"
android:ems="10"
>
</TextView>
<TextView
android:id="@id/view_login_account_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/view_login_account_username"
android:layout_alignParentRight="true"
android:layout_below="@id/view_login_account_username"
android:ems="10"
android:text="@string/string_view_login_account_progress_text" />
</RelativeLayout>
<!-- layout/view_login_account_stored_password.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@id/view_login_account_profileimage"
android:layout_width="@dimen/view_login_account_profileimage_width"
android:layout_height="@dimen/view_login_account_profileimage_height"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/string_view_login_account_profileimage_contentdescription"
android:scaleType="fitCenter"
android:src="@drawable/img_kb" />
<TextView
android:id="@id/view_login_account_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/view_login_account_profileimage"
android:ems="10"
>
</TextView>
<TextView
android:id="@id/view_login_account_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/view_login_account_username"
android:layout_alignParentRight="true"
android:layout_below="@id/view_login_account_username"
android:ems="10"
android:text="@string/string_view_login_account_password_text"
/>
</RelativeLayout>
<!-- layout/view_login_account_stored_username.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@id/view_login_account_profileimage"
android:layout_width="@dimen/view_login_account_profileimage_width"
android:layout_height="@dimen/view_login_account_profileimage_height"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/string_view_login_account_profileimage_contentdescription"
android:scaleType="fitCenter"
android:src="@drawable/img_kb" />
<TextView
android:id="@id/view_login_account_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/view_login_account_profileimage"
android:ems="10"
android:hint="@string/string_view_login_account_username_hint" >
<requestFocus />
</TextView>
<EditText
android:id="@id/view_login_account_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/view_login_account_username"
android:layout_alignParentRight="true"
android:layout_below="@id/view_login_account_username"
android:ems="10"
android:hint="@string/string_view_login_account_password_hint"
android:inputType="textPassword" />
<CheckBox
android:id="@id/view_login_account_storecredentials"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/view_login_account_password"
android:layout_toRightOf="@id/view_login_account_profileimage"
android:text="@string/string_view_login_account_storecredentials_text" />
<Button
android:id="@id/view_login_account_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/view_login_account_storecredentials"
android:text="@string/string_view_login_account_submit_text" />
</RelativeLayout>
代码:
代码都没有实现onSaveInstanceState和onConfigurationChange。 (整个报价将太长,imho,> 1.000行代码)
public class MessengerActivity extends SherlockFragmentActivity {
// -------------------------------------------------------------------------
FrameLayout fragmentSidebar = null;
FrameLayout fragmentContent = null;
Content content = null;
// -------------------------------------------------------------------------
boolean hasSidebar = false;
// -------------------------------------------------------------------------
public void onCreate(Bundle savedInstanceState) {
// ---------------------------------------------------------------------
super.onCreate(savedInstanceState);
// ---------------------------------------------------------------------
content = Content.getInstance(this);
// ---------------------------------------------------------------------
setContentView(R.layout.main);
// ---------------------------------------------------------------------
captureFragmentViews();
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
if (savedInstanceState == null) {
// -----------------------------------------------------------------
gotoLogin();
// -----------------------------------------------------------------
}
else {
// -----------------------------------------------------------------
// -----------------------------------------------------------------
}
// ---------------------------------------------------------------------
}
// -------------------------------------------------------------------------
// Disabled through android manifest at the moment
@Override
public void onConfigurationChanged(Configuration newConfig) {
// ---------------------------------------------------------------------
super.onConfigurationChanged(newConfig);
// ---------------------------------------------------------------------
Log.d(getClass().getSimpleName(), "onConfigurationChanged");
// ---------------------------------------------------------------------
}
// -------------------------------------------------------------------------
public void captureFragmentViews() {
// ---------------------------------------------------------------------
fragmentSidebar = (FrameLayout) findViewById(R.id.main_fragment_sidebar);
fragmentContent = (FrameLayout) findViewById(R.id.main_fragment_content);
// ---------------------------------------------------------------------
if ((fragmentSidebar != null) && (fragmentContent != null)) {
hasSidebar = true;
}
else {
hasSidebar = false;
}
// ---------------------------------------------------------------------
}
// -------------------------------------------------------------------------
public void gotoLogin() {
// ---------------------------------------------------------------------
Fragment fragment = SherlockFragment.instantiate(this,
LoginFragment.class.getName());
// ---------------------------------------------------------------------
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (hasSidebar) {
ft.add(R.id.main_fragment_content, fragment);
fragmentSidebar.setVisibility(View.GONE);
}
else {
ft.add(R.id.main_fragment_content, fragment);
}
// ---------------------------------------------------------------------
ft.commit();
getSupportFragmentManager().executePendingTransactions();
// ---------------------------------------------------------------------
}
// -------------------------------------------------------------------------
public void gotoSignup() {
// TODO Auto-generated method stub
}
// -------------------------------------------------------------------------
public void gotoContactList(int filterId) {
// TODO Auto-generated method stub
}
// -------------------------------------------------------------------------
public void gotoConversation(int userId) {
// TODO Auto-generated method stub
}
// -------------------------------------------------------------------------
public void gotoOnlineStatusList(int categoryId) {
// TODO Auto-generated method stub
}
// -------------------------------------------------------------------------
public void gotoSettings(int categoryId) {
// TODO Auto-generated method stub
}
// -------------------------------------------------------------------------
}
我检查主活动中是否设置了savedInstanceState,因此我的活动已经准备好了#34;在片段娱乐方面。
片段在创建适配器并将其附加到列表视图之前检查适配器是否已存在。 但是:就我的理解而言,即使&#34;重新创建&#34;片段使用相同的适配器,&#34;默认情况下&#34;所有getViews()都被调用,因此使用&#34; resetted&#34;创建一个新的视图实例。内容。
AccountAdapter获得专用的&#34;帐户&#34; - 具有来自数据库的用户ID,用户名和密码的对象。此外,&#34;帐户&#34;可以扩展以保存其他数据,例如缓存视图。
我的想法是实现这样的getView:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// ---------------------------------------------------------------------
final Account account = (Account) getItem(position);
View view = null;
// ---------------------------------------------------------------------
if (account == null) {
return view;
}
// ---------------------------------------------------------------------
if (account.view != null) {
return account.view;
}
// ---------------------------------------------------------------------
[...]
}
但后来我注意到,AccountAdapter在娱乐中无法生存,Android重新实例化了LoginFragment类。
我需要什么:
答案 0 :(得分:1)
每次Fragment
调用onCreate()
方法时,每次添加Activity
时,您的代码都不清楚。
只有在第一次创建Fragment
get时,才应添加Fragment
或第一个Activity
。
if(savedInstanceState==null){
addFragment();
}
如果这不是null
,则会重新创建Activity
,并且还会重新添加您之前的Fragments
。如果Fragments
中有backstack
个Activity
,他们将全部放回setRetainInstance(true);
。
要将数据保留在片段中,您应该使用Fragments
onCreate()
方法上的onSaveInstanceState(Bundle bundle)
,或使用{{1}}保存特定数据。