I've been having this issue for a while and can't figure out how to fix it. I have one activity with four fragments. I'm having an issue with one fragment. I have a button on this fragment that when pressed needs to add another button. This works. However when I try to add another new button, it just takes over the place of the first. Also when I switch to another tab and then back to the one with this button, the created button is gone.
Accounts.java
package com.mycompany.mobilefinancetracker;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class Accounts extends Fragment {
private AccountsController accounts;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.accounts,null);
accounts = new AccountsController(getActivity());
accounts.open();
rootView.findViewById(R.id.add_account_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent addAccountScreenIntent = new Intent(getActivity(),AddAccount.class);
final int result = 1;
addAccountScreenIntent.putExtra("callingActivity","MainActivity");
startActivityForResult(addAccountScreenIntent,result);
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Button acctDet = new Button(getActivity());
LinearLayout accts = (LinearLayout) getView().findViewById(R.id.linlay);
//Button newAccountDetails = (Button) getView().findViewById(R.id.account_val1);
String a_name = String.format("%8s",data.getStringExtra("acct_name"));
String a_type = String.format("%10s",data.getStringExtra("acct_type"));
String a_amount = String.format("%7s",data.getStringExtra("acct_amount"));
String a_limit = String.format("%10s",data.getStringExtra("acct_limit"));
accounts.insert(a_name.trim(),a_type.trim(),a_amount.trim(),a_limit.trim());
//newAccountDetails.setText(" ");
//newAccountDetails.append(""+a_name+ " " + a_type + " " + a_amount + "" + a_limit);
acctDet.setText(" ");
acctDet.append(""+a_name+ " " + a_type + " " + a_amount + "" + a_limit);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
acctDet.setLayoutParams(buttonParams);
accts.addView(acctDet);
acctDet.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent editAccountScreenIntent = new Intent(getActivity(),EditAccount.class);
final int result = 1;
editAccountScreenIntent.putExtra("callingActivity","MainActivity");
startActivityForResult(editAccountScreenIntent,result);
}
});
}
}
Accounts.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<Button
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_account"
android:textSize="28sp"
android:id="@+id/add_account_button"
android:onClick="onAddAccountClick"
/>
<LinearLayout
android:id="@+id/linlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
I have to assume that it is an issue with my onCreateView. I guess that just inflating the xml doesn't keep the button after it's been added? Or maybe there is a way to save the current fragment state and load that instead of inflating the xml?
If anyone could explain a way to fix this issue, I would greatly appreciate it!