I have weird glitch that I have hard time explaining with words. So a recorded this video to show it. FAB opens a activity which has a EditText and a confirm button. When you press the confirm button the name that written to EditText will be added to a RecyclerView in Library fragment. But if you don't write anything to EditText and press confirm it will add "Johan". But here is the thing, if you didn't even click to EditText it will add Johan to ArrayList but it won't show it until you add another name "properly" or restart the Players activity.
Library Fragment:
public class SetupplayerLibraryFragment extends Fragment {
private static String TAG = "SetupplayerLibraryFragment";
static View view;
InternalOperator IO;
RecyclerView libraryList;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager manager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.fragment_setupplayer_library, container, false);
libraryList = view.findViewById(R.id.libraryList);
libraryList.addItemDecoration(new DividerItemDecoration(libraryList.getContext(), DividerItemDecoration.VERTICAL));
manager = new LinearLayoutManager(getActivity());
adapter = new PlayerAdapter(IO.players);
libraryList.setLayoutManager(manager);
libraryList.setAdapter(adapter);
return view;
}
}
New Player Activity:
public class SetupplayerAddActivity extends AppCompatActivity {
private static String TAG = "SetupplayerAddActivity";
EditText nameText;
Button confirmButton;
Toolbar toolbar;
InternalOperator IO;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setupplayer_add);
nameText = findViewById(R.id.text_add_name);
confirmButton = findViewById(R.id.button_add_confirm);
confirmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (nameText.getText().length() != 0) {
if (ContextCompat.checkSelfPermission(SetupplayerAddActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
nameText.clearComposingText();
ExternalOperator.savePlayers(nameText.getText().toString());
IO.players.add(new Player(nameText.getText().toString(), 0));
finish();
} else {
Toast.makeText(SetupplayerAddActivity.this, "Permission to use storage is needed for this action!", Toast.LENGTH_SHORT).show();
}
} else {
if (ContextCompat.checkSelfPermission(SetupplayerAddActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
ExternalOperator.savePlayers("Johan");
IO.players.add(new Player("Johan", 0));
finish();//Activityi kapıyor
} else {
Toast.makeText(SetupplayerAddActivity.this, "You are really the Johan! Radiance save us!", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
I removed some of the useless codes to make it more clear.