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!

Add button
1. Setup new layout just with button
2. When adding button inflate this new layout to reduce all that mess with LinearLayout.LayoutParams
3. Then add button to parent (LinearLayout)

Buttons disapeared on fragment return - you need to apply some persistance to record your change, which should happen onPause and re-initiaded with onResume. You could do this with bundle that you pass around. However this will become more complicated as you move around app and other needs to be past to other components.
So you need to decide what storage is best for you http://developer.android.com/guide/topics/data/data-storage.html. For start you may want to use SharedPreferences, if you have more complex structure database would be better.

Thank you very much for your reply!

Just a quick question though. I do have a databse that will hold the four pieces of data that is displayed on each button. So in onResume I could simply iterate through the database and create a button/button handler for each item?

Yes, you can

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.