在从其他活动重新开始活动时,该活动的LinearLayout为空白

问题描述 投票:-1回答:1

我的Main活动正常显示,应在应用程序启动时呈现。它检索数据库条目并呈现它们。同样,当启动另一个活动Edit活动并使用“后退”按钮返回时,所有操作均如上所述执行。但是,当我从Edit活动执行此代码时:

Intent intent = new Intent(this, Main.class);

intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);

Main活动为空白,主要活动的代码为:

public class Main extends Base {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initDB(); // Just a database check if it is there, works always
    initFAB(); // FAB is visible as normal all the time, also when coming from Edit
    processIntent();
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
}

@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    processIntent();
}

/**
 * Processes the intent.
 */
private void processIntent() {
    Intent intent = getIntent();

    if (intent.getAction().equals(Intent.ACTION_MAIN) || intent.getAction().equals(Intent.ACTION_VIEW)) {
        if (intent.hasExtra(Intent.EXTRA_REPLACING))
            Toast.makeText(this, intent.getIntExtra(Intent.EXTRA_REPLACING, 0), Toast.LENGTH_LONG).show();
        else if (intent.hasExtra(Intent.EXTRA_RETURN_RESULT))
            Toast.makeText(this, intent.getStringExtra(Intent.EXTRA_RETURN_RESULT), Toast.LENGTH_LONG).show();

        queryAllNotes();
    }
  }
}

[我可以访问Main活动在另一个类中进行渲染,现在渲染代码是由于以下注释的调试尝试,queryAllNotes()从数据库中获取了参数notes,这是可行的:

/**
 * Renders all note wrappers.
 */
public void all(ArrayList<Note> notes) {
    final boolean noneFound = notes.size() < 1;

    if (noneFound) // Remove views
        remove();

    main.updateNoNotesText(noneFound);

    final ScrollView scrollView = remove();
    final LinearLayout linearLayout = new LinearLayout(main);

    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    scrollView.addView(linearLayout);

    for (int i = 0; !noneFound && i < notes.size(); i++) {
        if (i < linearLayout.getChildCount() && i < links.size()) { // Reuse views
            links.get(i).setNote(notes.get(i), i);
        } else { // Create new views
            LayoutInflater.from(main).inflate(R.layout.note, linearLayout, true);
            links.add(new NoteViewLink(notes.get(i), (RelativeLayout) linearLayout.getChildAt(linearLayout.getChildCount() - 1), i));
        }
    }

    // DEBUGGING:
    //        scrollView.setVisibility(View.GONE);
    //        scrollView.setVisibility(View.VISIBLE);
    //        scrollView.invalidate();
    //        scrollView.refreshDrawableState();

    for (int i = 0; i < scrollView.getChildCount(); i++) {
        LinearLayout v = (LinearLayout) scrollView.getChildAt(i);

        //            v.setVisibility(View.GONE);
        //            v.setVisibility(View.VISIBLE);
        //            v.invalidate();

        for (int j = 0; j < v.getChildCount(); j++) {
            RelativeLayout rl = (RelativeLayout) v.getChildAt(j);
            Log.d("debug", j + " RL: " + isShown(rl) + " " + rl.getVisibility() + "/" + View.VISIBLE + " LL: " + isShown(v) + " " + v.getVisibility() + "/" + View.VISIBLE);
            // App Start Log Output:
            // D/debug: 0 RL: true 0/0 LL: true 0/0
            // ...
            // D/debug: 50 RL: true 0/0 LL: true 0/0
            // Main Activity Start from Edit activity Log Output:
            // D/debug: 0 RL: false 0/0 LL: false 0/0
            // ...
            // D/debug: 50 RL: false 0/0 LL: false 0/0
        }
    }

    //        scrollView.requestLayout();
}

public boolean isShown(View current) {
    //noinspection ConstantConditions
    do {
        if (current.getVisibility() != View.VISIBLE) {
            return false;
        }
        ViewParent parent = current.getParent();
        if (parent == null) {
            return false; // We are not attached to the view root
        }
        if (!(parent instanceof View)) {
            return true;
        }
        current = (View) parent;
    } while (current != null);

    return false;
}

/**
 * Removes all views.
 *
 * @return For convenience the note scroll view.
 */
public ScrollView remove() {
    final ScrollView scrollView = main.findViewById(R.id.scrollView);
    scrollView.removeAllViews();
    links.clear();
    return scrollView;
}

.xmlMain是:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone">

    <!--LinearLayout is created programmatically in all()-->
   </ScrollView>
 </RelativeLayout>

我从Android的isShown(View current)类复制了View方法,以便能够逐步进行递归,它一直到DecorView,它会在if中输入第一个isShown(View current),然后返回false,对我来说没有任何意义,所有上面注释的代码均无效,也对我的LinearLayout及其子级RelativeLayouts无效。

[从Main活动中启动Main时如何显示Edit活动的内容?我不知道有什么区别,它在应用程序启动时从数据库中查询所有notes,而应该在应用程序启动时相应地呈现它们。我不必触摸DecorView,而只需触摸ScrollView,包含的LinearLayout或它的子级RelativeLayouts,它们都可见并且可以在应用程序启动时使用。

android android-activity android-linearlayout onresume
1个回答
0
投票

我最终只是不删除活动更改的视图,而是将新更改的视图添加到其他视图。

© www.soinside.com 2019 - 2024. All rights reserved.