android - 活动重启后某些视图不“可见”

时间:2013-10-28 17:56:51

标签: android android-activity restart

Hello Android开发者,

我在活动重启后面临一些奇怪的观点行为 - 有一些观点不是“可见的”,但它们是在触摸动作上进行布局和反应。

我正在努力让应用程序遵循Android生命周期指南。我将Activity放到后台,让系统关闭我的活动。然后我导航回我的活动,重新创建。数据损坏没有问题(每次更改时都保存在db中),但视图是。有一些正确显示,但 TableView 内的所有内容(在 ScrollView 中)都不可见。如果我在任何未显示的视图上调用 getVisibility(),我会看到它是可见的。正如我上面提到的,视图不是“可见的”,而是对Touch和滚动事件做出反应,就像它们被正确显示一样。

这也很难(不可能)进行调试,因为当应用程序关闭时,debbuger会断开连接。但无论如何,娱乐遵循相同的方法回调 - onCreate() onStart() onResume(),所以一旦创建,为什么会出现问题其他时间?对我来说唯一的区别是,当重新创建Activity时, onCreate(Bundle)中存在 null Bundle ;重新创建Activity时, not null 按系统。如果活动仅停止(在后台)但未被系统关闭,则一切正常。

我还尝试覆盖 onSaveInstanceState() onRestoreInstanceState(),不需要保存和恢复(不调用超级实现),但它没有任何效果。

我正在使用Android 4.1.1和模拟器2.1。

有没有人有任何想法?

谢谢Ales

以下是(链接到)截图,以便更好地解决这个问题:
Before activity is closed by system
After activity is recreated

以下是布局文件:          

    <!-- Header --> 
    <LinearLayout
        android:id="@+id/startlistHeaderLayout" xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        >
        <TextView android:id="@+id/textStartlistName"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:lines="1"
            android:gravity="left"
            android:text=""
            />
        <TextView android:id="@+id/textStarttime"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:lines="1"
            android:gravity="right"
            android:text=""
            android:layout_weight="1"
            />
    </LinearLayout>

    <!-- Results --> 
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@id/resultsScrollView"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:saveEnabled="false"
        >
        <TableLayout
            android:id="@id/timingLapTableLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="2"
            android:shrinkColumns="2"
            android:saveEnabled="false"
            >
            <!-- rows are added in code -->
        </TableLayout>
    </ScrollView>
</LinearLayout>

<!-- R.layout.timing_row -->
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/timingTableRow"
    android:saveEnabled="false"
    >
    <TextView
        android:id="@id/textTimingPosition"
        android:text=""
        android:gravity="right"
        android:paddingLeft="4dip"
        android:saveEnabled="false"
        />
    <TextView
        android:id="@id/textTimingBib"
        android:text=""
        android:gravity="right"
        android:paddingLeft="1dip"
        android:saveEnabled="false"
        />
    <TextView
        android:id="@id/textTimingName"
        android:text=""
        android:lines="1"
        android:ellipsize="end"
        android:gravity="left"
        android:paddingLeft="1dip"
        android:paddingRight="5dip"
        android:saveEnabled="false"
        />
    <TextView
        android:id="@id/textTimingBehind1"
        android:text=""
        android:gravity="right"
        android:paddingRight="5dip"
        android:saveEnabled="false"
        />
    <TextView
        android:id="@id/textTimingBehind2"
        android:text=""
        android:gravity="right"
        android:paddingRight="5dip"
        android:saveEnabled="false"
        />
</TableRow>

以下是活动代码的一部分:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    loadPreferencesTimingTheme();
    if (ownTheme) {
        this.setTheme(theme);
    } else {
        this.setTheme(MainActivity.theme);
    }
}

protected void onStart() {
    super.onStart();
    View lapView;
    View v;
    int sid;
    setContentView(R.layout.startlist_edit);
    sid = getIntent().getIntExtra(TimingActivity.STARTLIST_ID, -1);
    if (sid == -1) {
        Toast.makeText(this, "Sorry, could not load startlist.", Toast.LENGTH_LONG);
        this.finish();
        return;
    }
    tdb = new TimingDB(this);
    sl = tdb.getStartlist(sid); // get Startlist from db into memory
    // hide unnecessary columns
    v = findViewById(R.id.timingLapTableLayout);
    ((TableLayout) v).setColumnCollapsed(0, true);
    loadStartlist(sl); // inflate rows with startlist data and set listeners
    // load the preferences and set preferences listener
    loadPreferences();
    PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(prefsChangeListener);
}

public void onStop() {
    super.onStop();
    PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(prefsChangeListener);
    if (tdb != null) tdb.close();
}

protected void onSaveInstanceState (Bundle outState) {
    // super.onSaveInstanceState(outState);
}

protected void onRestoreInstanceState (Bundle savedInstanceState) {
    // super.onRestoreInstanceState(savedInstanceState);
}

protected void onDestroy() {
    super.onDestroy();
    if (tdb != null) tdb.close();
}

private void loadStartlist(StartList sl) {
    // set startlist name & time
    View v = findViewById(R.id.textStarttime);
    ((TextView) v).setText(SimpleDateFormats.ddmmyyyyhhmmss.format(new Date(sl.getStartTime())));
    v = findViewById(R.id.textStartlistName);
    ((TextView) v).setText(sl.getName());
    // set header onclicklistener
    v = findViewById(R.id.startlistHeaderLayout);
    v.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // show dialog to change startlist name and time
            Bundle args = new Bundle();
            args.putString(KEY_NAME, ((TextView) findViewById(R.id.textStartlistName)).getText().toString());
            args.putString(KEY_STTIME, ((TextView) v.findViewById(R.id.textStarttime)).getText().toString());
            if (Build.VERSION.SDK_INT < 8) {
                dialogBundle = args;
                showDialog(DIALOG_EDIT_STARTLIST);
            } else {
                showDialog(DIALOG_EDIT_STARTLIST, args);
            }
        }
    });
    // add competitors to startlist
    int cnt = sl.getCompetitorsCount();
    for (int i = 0; i < cnt; i++) {
        Competitor c = sl.getCompetitorByIndex(i);
        appendStartlistRow(c);
    }
}

public void appendStartlistRow(final Competitor c) {
    TableRow inflatedView = (TableRow) getLayoutInflater().inflate(R.layout.timing_row, null);
    inflatedView.setTag(TAG_COMPETITOR, c);
    inflatedView.setTag(new Integer(c.getBib()));
    inflatedView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TableLayout tl = (TableLayout) findViewById(R.id.timingLapTableLayout);
            Bundle args = new Bundle();
            args.putInt(KEY_INDEX, tl.indexOfChild(v));
            if (Build.VERSION.SDK_INT < 8) {
                dialogBundle = args;
                showDialog(DIALOG_EDIT_COMPETITOR);
            } else {
                showDialog(DIALOG_EDIT_COMPETITOR, args);
            }
        }
    });
    // set up texts
    TextView text = (TextView) inflatedView.findViewById(R.id.textTimingBib);
    text.setText(c.getBib() + "");
    text = (TextView) inflatedView.findViewById(R.id.textTimingName);
    text.setText(c.getName());
    text = (TextView) inflatedView.findViewById(R.id.textTimingBehind1);
    text.setText(getFormatedStartTime(sl, c, Settings.STARTTIME_ABSOLUTE));
    text = (TextView) inflatedView.findViewById(R.id.textTimingBehind2);
    text.setText(getFormatedStartTime(sl, c, Settings.STARTTIME_RELATIVE));
    // append the row
    appendStartlistRow((ViewGroup) inflatedView);
}

public void appendStartlistRow(ViewGroup row) {
    // set background and text colors
    if (((((Integer) row.getTag()).intValue()) % 2) == Defs.VIEW_EVEN) {
        row.setBackgroundColor(Defs.COLOR_BACKGROUND_EVEN);
        for (int j = 0; j < row.getChildCount(); j++) {
            ((TextView) (row.getChildAt(j))).setTextColor(Defs.COLOR_TEXT_EVEN);
        }
    } else {
        row.setBackgroundColor(Defs.COLOR_BACKGROUND_ODD);
        for (int j = 0; j < row.getChildCount(); j++) {
            ((TextView) (row.getChildAt(j))).setTextColor(Defs.COLOR_TEXT_ODD);
        }
    }
    ((TableLayout) findViewById(R.id.timingLapTableLayout)).addView(row);
}

Defs.java:

public class Defs {
    protected static final int VIEW_EVEN = 0;
    protected static final int VIEW_ODD = 1;
    protected static int COLOR_BACKGROUND_EVEN;
    protected static int COLOR_BACKGROUND_ODD;
    protected static int COLOR_BACKGROUND_SELECTED;
    protected static int COLOR_BACKGROUND_SPYED;
    protected static int COLOR_TEXT_ODD;
    protected static int COLOR_TEXT_EVEN;
    protected static int COLOR_TEXT_SELECTED;
    protected static int COLOR_TEXT_SPYED;

    private static boolean isInitialized = false;

    protected static void init(Context c) {
        if (isInitialized) return;
        COLOR_BACKGROUND_EVEN = c.getResources().getColor(R.color.background_darker);
        COLOR_BACKGROUND_ODD = c.getResources().getColor(R.color.background_lighter);
        COLOR_BACKGROUND_SELECTED = c.getResources().getColor(R.color.background_selected);
        COLOR_BACKGROUND_SPYED = c.getResources().getColor(R.color.background_spyed);
        COLOR_TEXT_ODD = c.getResources().getColor(R.color.text_lighter);
        COLOR_TEXT_EVEN = c.getResources().getColor(R.color.text_darker);
        COLOR_TEXT_SELECTED = c.getResources().getColor(R.color.text_selected);
        COLOR_TEXT_SPYED = c.getResources().getColor(R.color.text_spyed);
        isInitialized = true;
    }
}

1 个答案:

答案 0 :(得分:0)

没有任何代码,它几乎不可能对问题本身说多少。但是我确实想说(对于一个很长的列表(比如你的似乎是基于屏幕截图),使用实际的ListView和自定义ArrayAdapter可能是更好的解决方案。滚动时ListView重新使用视图,从而减少不必要的视图创建。此外,一旦您学习了如何使用ListView,您可能会发现它比手动添加表行更容易。如果您不熟悉这些内容,请在此处查看教程: http://www.vogella.com/articles/AndroidListView/article.html

如果您决定从表格更改为ListView,也许问题(无论是什么)也会消失。你永远都不会知道。 :)