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.