我正在开发一个使用 ActionBarSherlock 和ViewPagerIndicator构建的 Android 应用程序,minSDK 为 8(Android 2.2),targetSDK 为 16(Android 4.1),使用 Android 兼容库。我有一个应用程序的消息传递部分,其中消息存储在 SQLite 数据库中,并且我正在使用 CommonsWare 的cwac-loaderex将游标加载器与 SQLite 数据库一起使用。当活动加载时,一切正常并显示消息,但是当我旋转设备时,它只显示一个带有加载循环的 ListFragment。
打开 LoaderManager.enableDebugLogging(true) 现有的加载器在我旋转时被重用,我还尝试了 onDestroy 中的 getSupportLoaderManager().destroyLoader(ID) 并且正在创建新的加载器,但我仍然有相同的结果,带有加载圈的 ListFragment . 在打开消息传递活动之前,我已经在两个方向上进行了测试,它们都工作正常,这只是当我在消息传递活动可见时旋转时出现的问题。我还尝试以纵向进入消息传递活动,单击一条消息将我带到另一个活动而不破坏消息传递活动,更改为横向,然后按回消息传递活动,一切都显示正常。
我无法弄清楚为什么在轮换后没有显示消息。我会非常感谢任何想法或帮助。我在下面包含了我的消息传递活动。
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SimpleCursorAdapter;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.commonsware.cwac.loaderex.acl.SQLiteCursorLoader;
import com.viewpagerindicator.TitlePageIndicator;
import com.lukekorth.DB.DatabaseHelper;
import com.lukekorth.Helpers.MessageFragment;
public class MyMessagesActivity extends SherlockFragmentActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
private FragmentAdapter mFragmentAdapter;
private ViewPager mPager;
private DatabaseHelper mDB;
private SimpleCursorAdapter mInboxCursorAdapter;
private SimpleCursorAdapter mArchiveCursorAdapter;
private MessageFragment[] content = new MessageFragment[2];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messages);
getSupportActionBar().setHomeButtonEnabled(true);
getSherlock().getActionBar().setDisplayHomeAsUpEnabled(true);
mDB = new DatabaseHelper(this);
content[0] = new MessageFragment();
content[1] = new MessageFragment();
/////////--------------------///////
LoaderManager.enableDebugLogging(true);
mInboxCursorAdapter = new SimpleCursorAdapter(this, R.layout.message_item, null, new String[] { DatabaseHelper.senderName,
DatabaseHelper.lastUpdate, DatabaseHelper.subject, DatabaseHelper.lastSnippet }, new int[] { R.id.name, R.id.date,
R.id.subject, R.id.snippet });
mArchiveCursorAdapter = new SimpleCursorAdapter(this, R.layout.message_item, null, new String[] { DatabaseHelper.senderName,
DatabaseHelper.lastUpdate, DatabaseHelper.subject, DatabaseHelper.lastSnippet }, new int[] { R.id.name, R.id.date,
R.id.subject, R.id.snippet });
content[0].setListAdapter(mInboxCursorAdapter);
content[1].setListAdapter(mArchiveCursorAdapter);
getSupportLoaderManager().initLoader(0, null, this);
getSupportLoaderManager().initLoader(1, null, this);
///////////////------------------////////////
mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
mFragmentAdapter.updateContent(content);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mFragmentAdapter);
TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(mPager);
}
@Override
public void onDestroy() {
super.onDestroy();
getSupportLoaderManager().destroyLoader(0);
getSupportLoaderManager().destroyLoader(1);
mDB.close();
}
@Override
public Loader<Cursor> onCreateLoader(int folder, Bundle arg1) {
String query = "select " + DatabaseHelper.threads + "." + DatabaseHelper.threadKey + "," + DatabaseHelper.subject +
"," + DatabaseHelper.archive + "," + DatabaseHelper.otherUser + "," + DatabaseHelper.lastUpdate + "," +
DatabaseHelper.lastSnippet + "," + DatabaseHelper.senderPicture + "," + DatabaseHelper.senderName + " from " +
DatabaseHelper.threads + " left join " + DatabaseHelper.senders + " on " + DatabaseHelper.threads + "." +
DatabaseHelper.otherUser + " = " + DatabaseHelper.senders + "." + DatabaseHelper.senderKey + " where " +
DatabaseHelper.archive;
if(folder == 0)
query += " = 0 order by ";
else
query += " = 1 order by ";
query += DatabaseHelper.lastUpdate + " desc";
return new SQLiteCursorLoader(this, mDB, query, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if(loader.getId() == 0)
mInboxCursorAdapter.swapCursor(cursor);
else
mArchiveCursorAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
if(loader.getId() == 0)
mInboxCursorAdapter.swapCursor(null);
else
mArchiveCursorAdapter.swapCursor(null);
}
}
class FragmentAdapter extends FragmentPagerAdapter {
protected static final String[] TITLE = new String[] { "Inbox", "Archive", };
private ListFragment[] mContent;
private int mCount = TITLE.length;
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
public void updateContent(MessageFragment[] content){
mContent = content;
}
@Override
public Fragment getItem(int position) {
return mContent[position % mContent.length];
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return FragmentAdapter.TITLE[position % TITLE.length];
}
}