Fragment is unstable in Android Studio
KARUNESH PALEKAR
I am a complete beginner in using Android Studio . Here in this code I am trying to implement a Fragment using a button imposed in the Toolbar .Basically, I want to display the fragment content when clicked on the add button.I want to take few inputs from the user from the fragment update it to Firebase and receive it from Firebase and retrieve it to a recycler view. When I tried to execute the code given below, the fragments blinks and then the app stops working.Any alternative to apply my process would be appreciated.
This is my activity file..
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;
public class history extends AppCompatActivity {
enter code here
EditText success;
private FrameLayout frameLayout;
private historyfrag historyfrag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Toolbar toolbar = findViewById(R.id.toolbarhistory);
setSupportActionBar(toolbar);
ActionBar ab =getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
frameLayout = findViewById(R.id.framelayut);
historyfrag = new historyfrag();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.toolbarhistorymenu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item){
switch (item.getItemId()) {
case R.id.print:
Toast.makeText(getApplicationContext(), "Print", Toast.LENGTH_LONG).show();
return true;
case R.id.add:
setupfragment(historyfrag);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setupfragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.framelayut,fragment);
fragmentTransaction.commit();
}
}
This is the xml file of the activity...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbarhistory"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorAccent"
android:elevation="6dp"
app:menu="@menu/toolbarhistorymenu"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="History" />
<FrameLayout
android:id="@+id/framelayut"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbarhistory" />
</RelativeLayout>
Found this error in the Logcat
java.lang.RuntimeException: com.example.myapplication.history@2fb40c4 must implement OnFragmentInteractionListener
at com.example.myapplication.historyfrag.onAttach(historyfrag.java:85)
at androidx.fragment.app.Fragment.performAttach(Fragment.java:2574)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:828)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1197)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1080)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:119)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
MaCoda
Change your class name to:
public class history extends AppCompatActivity implements YourFragmentClassName.OnFragmentInteractionListener
Inside fragment class you should have:
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}