Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – LinearLayout does not show children after returning to the activity

Posted on November 14, 2022

Solution 1 :

As kelvin suggested, I’ve removed setContenView from activityResult and this helped. However, I do not understand how it works, but it will do for me for now.

Solution 2 :

setContentView(R.layout.activity_main); sets the XML (Layout ) for your activity when you call setContentView it will inflate the Layout on the Screen. Now when you are calling it again in onActivityResult You are inflating the view again.

Also I would suggest you to use ListView / RecyclerView for showing lists.

Problem :

Trying to create an app which makes lists. To create a new element in my list I want to use another activity. I do it like that:

public void onNewTask(View view)
{
    Intent newTask = new Intent(MainActivity.this, NewTaskActivity.class);

    MainActivity.this.startActivityForResult(newTask, 0);
}

After returning to the main activity with results like this:

public void onSave(View view){
    Intent main = new Intent();

    String[] data;

    //getting data here//

    main.putExtra("New_task", data);
    setResult(RESULT_OK, main);

    finish();
}

After that, the linear layout does not show any children, though if I debug my code, I can see that the layout still has them inside. I am adding children programmatically, like this:

LinearLayout linearLayout = findViewById(R.id.toDoLayout); //is in OnCreate()

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams //is in OnCreate()
         (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout newLayout = new LinearLayout(this);
newLayout.setOrientation(LinearLayout.HORIZONTAL);

TextView newNumberTextView = new LargeTextViewWithMargins(this);
newNumberTextView.setText("Test");
newLayout.addView(newNumberTextView);

linearLayout.addView(newLayout, layoutParams);

And the onActivityResult is now in this form:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    setContentView(R.layout.activity_main);
}

I’ve found a similar question here. Does anyone know what may cause such a situation and how it is better to overcome it?

Comments

Comment posted by kelvin

can you try after removing setcontentview from activityresult?

Comment posted by Paliy Mikle

Yep, that helped. Thank you! I definitely need to learn more about context.

READ  [ANSWERED] ubuntu 16.04 - Flutter doctor --android-licenses not finding sdkmanager without Android Studio
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress