how do call kotlin fragment from java fragment class - android

This is ActivityUtil code
public class ActivityUtil {
public static void addFragmentToActivity(#NonNull FragmentManager fragmentManager,
#NonNull Fragment fragment, int frameId, String fragmentTag) {
//Fragment fragment1=fragmentManager.findFragmentByTag(fragmentTag);
Preconditions.checkNotNull(fragmentManager);
Preconditions.checkNotNull(fragment);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(frameId, fragment, fragmentTag);
transaction.addToBackStack(fragmentTag);
transaction.commit();
}
}
This is main fragment class here i want add /replace fragment transaction by using kotlin fragment i.e,CameraFragment Class
I Already used Photofragment class now i want change the kotlin fragment, how do achieve this scenario?
public class ExpLotBcodeFragment extends Fragment{
public ExpLotBcodeFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}
#SuppressLint("ClickableViewAccessibility")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_exp_lot_bcode, container, false);
}
#OnClick({R.id.scan})
public void onClick(View view) {
switch (view.getId()) {
case R.id.scan:
//here i want to add kotlin call CameraFragment class
//This is old call
ActivityUtil.addFragmentToActivity(getFragmentManager(),
photoFragment, R.id.frame_content, "photoFragment");
break;
}
This is kotlin class
class CameraFragment : Fragment() {
companion object {
fun newInstance(): CameraFragment {
return CameraFragment ()
}
}
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_exp_lot_bcode, container, false)
}
}
From CameraFragment class i want to add/replace to ExpLotBcodeFragment class ?

Either you have to call CameraFragment.Companion.newInstance() or directly call new CameraFragment() to get instance of CameraFragment.
Check below:
#OnClick({R.id.scan})
public void onClick(View view) {
switch (view.getId()) {
case R.id.scan:
CameraFragment cameraFragment = CameraFragment.Companion.newInstance();
ActivityUtil.addFragmentToActivity(getFragmentManager(),
cameraFragment, R.id.frame_content, "cameraFragment");
break;
}
}

Related

Nested preferences inside fragment (from bottom navigation) - back stack not working

Everything is working, except that I am not able to go back to the settings fragment. Why is the Fragment not called, when clicking on the back button?
Structure:
MainActivity -> SettingsFragment (inside NavController) -> Preferences Overview -> First Preference Category
SettingsFragment.java
public class SettingsFragment extends Fragment implements
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback{
private SettingsViewModel settingsViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
settingsViewModel = ViewModelProviders.of(this).get(SettingsViewModel.class);
final View root = inflater.inflate(R.layout.settings_activity, container, false);
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SetupSettingsFragment(), "SetupSettingsFragment")
.addToBackStack(null)
.commit();
return root;
}
public static class SetupSettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences_overview, rootKey);
}
}
#Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
final Bundle args = pref.getExtras();
final Fragment fragment = getActivity().getSupportFragmentManager().getFragmentFactory().instantiate(
getActivity().getClassLoader(),
pref.getFragment());
fragment.setArguments(args);
fragment.setTargetFragment(caller, 0);
int setupSettingsFragment = getFragmentManager().findFragmentByTag("SetupSettingsFragment").getId();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(setupSettingsFragment, fragment)
.addToBackStack(null)
.commit();
return true;
}
FirstPreferenceCategory.java
public class FirstPreferenceCategory extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.PreferenceOverview, rootKey);
}}
}
PreferenceOverview.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="preferenceScreenOverview"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Preference
app:title="FirstPreferenceCategory"
app:fragment="com.ui.FirstPreferenceCategory"/>
Edit:
I also moved the onPreferenceStartManager and the SetupSettingsFragment to the MainActivity, but still there is something wrong with the back stack.

How to restore savedInstanceState of fragment?

I was able to save outState as follows but was unable to restore when I land on this AttendanceFragment.cs second time.
public override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
dataGotFromServer = JsonConvert.SerializeObject(dataList);
outState.PutString(KEY_OUTSTATE, dataGotFromServer);
}
I tried here to restore but could not get it
public override void OnViewStateRestored(Bundle savedInstanceState)
{
base.OnViewStateRestored(savedInstanceState);
if(savedInstanceState!=null)
{
var result = savedInstanceState.GetString(KEY_OUTSTATE, dataGotFromServer);
}
}
and also I tried on CreateView(), OnActivityCreated() and On Create() but unsuccessfull to restore.
And my code for fragment replacement is as
public void ReplaceFragment(Context context, Fragment newFragment, string TAG)
{
Android.Support.V4.App.FragmentManager fragmentManager = ((FragmentActivity)context).SupportFragmentManager;
Android.Support.V4.App.FragmentTransaction ft = fragmentManager.BeginTransaction();
ft.Replace(Resource.Id.HomeFrameLayout, newFragment);
ft.AddToBackStack(TAG);
ft.Commit();
}
Edited:
This is how I call this fragment
case (Resource.Id.nav_attendance):
var role = session.GetUserDetails().Get(SessionManagement.KEY_ROLE).ToString();
if (role=="Student")
{
Fragment attendanceTabFragment = new AttendanceTabFragment();
customFragment.ReplaceFragment(this, attendanceTabFragment,typeof(AttendanceTabFragment).Name);
}else
{
Fragment attendanceFragment = new AttendanceFragment();
customFragment.ReplaceFragment(this, attendanceFragment, typeof(AttendanceFragment).Name);
}
Any idea or sample code much appreciated.
Thank you.
Unless the Activity that contains the Fragment get disposed, Fragment's OnSaveInstanceState is not going to be called.
In a situation were you are swapping Fragments in and out, using Fragment.Arguments is an option instead of a singleton/static var...
re: getArguments / setArguments
In using arguments:
Create a new Bundle in the Fragment constructor and assign it to
Arguments
In the OnPause override update the Arguments/Bundle
with the items you need to save.
In the OnResume override read the
Arguments/Bundle items that you need to restore.
Example Fragment:
public class Fragment1 : Fragment
{
public Fragment1(System.IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer)
{
CreateArgumentBundle();
}
public Fragment1()
{
CreateArgumentBundle();
}
void CreateArgumentBundle()
{
Arguments = new Bundle();
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.Frag1, container, false);
}
public override void OnPause()
{
base.OnPause();
Arguments.PutString("someKey", "StackOverflow");
}
public override void OnResume()
{
base.OnResume();
var someKeyString = Arguments.GetString("someKey", "someDefaultString(new bundle)");
Log.Debug("SO", someKeyString);
}
}
In your ReplaceFragment make sure that you are assigning the TAG in the Replace call:
public void ReplaceFragment(Context context, Fragment newFragment, string TAG)
{
SupportFragmentManager
.BeginTransaction()
.Replace(Resource.Id.fragmentContainer, newFragment, TAG)
.AddToBackStack(TAG)
.Commit();
}
Before calling your ReplaceFragment, check to see if the Fragment exists (FindFragmentByTag) before creating a new one, this example just swaps two fragments in and out and only creates new ones if the manager does not contain one:
button.Click += delegate
{
toggle = !toggle;
var frag = SupportFragmentManager.FindFragmentByTag(toggle ? "frag1" : "frag2");
frag = frag ?? ( toggle ? (Fragment)new Fragment1() : (Fragment)new Fragment2() );
ReplaceFragment(this, frag, toggle ? "frag1" : "frag2");
};
Note: You will still need to handle the other Fragment lifecycle events in the case that the hosting Activity is recycled:
Fragment LifeCycles

Android :How to define dialog fragment that is accessed by multiple activities?

I have an Activity1 that shows a custom dialogfragment- ExampleDialog. I have implemented a listener, SubmitDialogListener for the dialogfragment to communicate with the activity using onSubmit(). In onSubmit(), I am showing some other dialogfragment. My question is, if I show the same ExampleDialog Fragment from Activity2, should Activity2 implement the SubmitDialogListener interface and implement the onSubmit() method in Activity2 again? Or is there a better way to define the onSubmit() method?
import android.support.v4.app.DialogFragment;
// ...
public class Activity1 extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
......
showDialog();
}
private void showDialog() {
FragmentManager fm = getSupportFragmentManager();
ExampleDialog exDialog = ExampleDialog.newInstance("Some Title");
exDialog.show(fm, "fragment_edit_name");
}
#Override
public void onSubmit() {
//open new fragments
}
}
public class ExampleDialog extends DialogFragment implements SubmitDialogListener {
public interface SubmitDialogListener {
void onSubmit();
}
public ExampleDialog() {
// Empty constructor required for DialogFragment
}
public static ExampleDialog newInstance(String title) {
ExampleDialog frag = new ExampleDialog();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_name, container);
.........
Button button = (Button) v.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SubmitDialogListener listener = (SubmitDialogListener) getActivity();
listener.onSubmit();
dismiss();
}
});
return view;
}
}
In this case the Activity should implement the SubmitDialogListener not the Dialog Fragment, like this:
public class Activity1 extends ActionBarActivity implements SubmitDialogListener {
#Override
public void onSubmit() {
//open new fragments
}
}
And if you want to use the same dialog fragment in multiple activities, it's generally best to create it in a separate class file.

Android: How to perform unit test for android fragment

I have an Android Tab Layout with swipeable views.
The file structure is as follows:
There is an activity class: TabMainActivity.java
Under this activity, there is a fragment class: bookLockerFragment.java
This fragment class is linked to an XML file which contains various buttons.
public class bookLockerFragment extends Fragment {
Button btnSis;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_booklocker, container, false);
btnSis = (Button) rootView.findViewById(R.id.btnSis);
}
.......
}
I have read up on vogella activity testing tutorials, but there is minimal information on unit testing for fragment.
I attempted to write some code but got stuck at this line:
transaction.add(R.layout.fragment_booklocker, fragment, "tag");
I get the following error:
The method add(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, booklockertest, String)
Code:
public class booklockertest extends
ActivityInstrumentationTestCase2 < TabMainActivity > {
private TabMainActivity mActivity;
Fragment fragment;
FragmentManager mFragmentManager;
public booklockertest() {
super(TabMainActivity.class);
// TODO Auto-generated constructor stub
}
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
}
private Fragment startFragment(booklockertest fragment) {
FragmentTransaction transaction =
mActivity.getSupportFragmentManager().beginTransaction();
transaction.add(R.layout.fragment_booklocker, fragment, "tag");
transaction.commit();
getInstrumentation().waitForIdleSync();
Fragment frag =
mActivity.getSupportFragmentManager().findFragmentByTag("tag");
return frag;
}
public void testFragment() {
booklockertest fragment = new booklockertest() {
//Override methods and add assertations here.
};
Fragment frag = startFragment(fragment);
}
}
stumble upon this, and you probably have figured it out already, but
private Fragment startFragment(booklockertest fragment) { ... }
...
public void testFragment() {
booklockertest fragment = new booklockertest() {
//Override methods and add assertations here.
};
Fragment frag = startFragment(fragment);
}
should be
private Fragment startFragment(bookLockerFragment fragment) { ... }
...
public void testFragment() {
bookLockerFragment fragment = new bookLockerFragment() {
//Override methods and add assertations here.
};
Fragment frag = startFragment(fragment);
}

How do I add a Fragment to an Activity with a programmatically created content view

I want to add a Fragment to an Activity that implements its layout programmatically. I looked over the Fragment documentation but there aren't many examples describing what I need. Here is the type of code I tried to write:
public class DebugExampleTwo extends Activity {
private ExampleTwoFragment mFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frame = new FrameLayout(this);
if (savedInstanceState == null) {
mFragment = new ExampleTwoFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(frame.getId(), mFragment).commit();
}
setContentView(frame);
}
}
...
public class ExampleTwoFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
Button button = new Button(getActivity());
button.setText("Hello There");
return button;
}
}
This code compiles but crashes at start, probably because my FragmentTransaction.add() is incorrect. What is the correct way to do this?
It turns out there's more than one problem with that code. A fragment cannot be declared that way, inside the same java file as the activity but not as a public inner class. The framework expects the fragment's constructor (with no parameters) to be public and visible. Moving the fragment into the Activity as an inner class, or creating a new java file for the fragment fixes that.
The second issue is that when you're adding a fragment this way, you must pass a reference to the fragment's containing view, and that view must have a custom id. Using the default id will crash the app. Here's the updated code:
public class DebugExampleTwo extends Activity {
private static final int CONTENT_VIEW_ID = 10101010;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frame = new FrameLayout(this);
frame.setId(CONTENT_VIEW_ID);
setContentView(frame, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
if (savedInstanceState == null) {
Fragment newFragment = new DebugExampleTwoFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(CONTENT_VIEW_ID, newFragment).commit();
}
}
public static class DebugExampleTwoFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
EditText v = new EditText(getActivity());
v.setText("Hello Fragment!");
return v;
}
}
}
Here is what I came up with after reading Tony Wong's comment:
public class DebugExampleTwo extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addFragment(android.R.id.content,
new DebugExampleTwoFragment(),
DebugExampleTwoFragment.FRAGMENT_TAG);
}
}
...
public abstract class BaseActivity extends Activity {
protected void addFragment(#IdRes int containerViewId,
#NonNull Fragment fragment,
#NonNull String fragmentTag) {
getSupportFragmentManager()
.beginTransaction()
.add(containerViewId, fragment, fragmentTag)
.disallowAddToBackStack()
.commit();
}
protected void replaceFragment(#IdRes int containerViewId,
#NonNull Fragment fragment,
#NonNull String fragmentTag,
#Nullable String backStackStateName) {
getSupportFragmentManager()
.beginTransaction()
.replace(containerViewId, fragment, fragmentTag)
.addToBackStack(backStackStateName)
.commit();
}
}
...
public class DebugExampleTwoFragment extends Fragment {
public static final String FRAGMENT_TAG =
BuildConfig.APPLICATION_ID + ".DEBUG_EXAMPLE_TWO_FRAGMENT_TAG";
// ...
}
Kotlin
If you are using Kotlin make sure to take a look at what the Kotlin extensions by Google provide or just write your own.
public class Example1 extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DemoFragment fragmentDemo = (DemoFragment)
getSupportFragmentManager().findFragmentById(R.id.frame_container);
//above part is to determine which fragment is in your frame_container
setFragment(fragmentDemo);
(OR)
setFragment(new TestFragment1());
}
// This could be moved into an abstract BaseActivity
// class for being re-used by several instances
protected void setFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, fragment);
fragmentTransaction.commit();
}
}
To add a fragment into a Activity or FramentActivity it requires a
Container. That container should be a "Framelayout", which can be
included in xml or else you can use the default container for that
like "android.R.id.content" to remove or replace a fragment in
Activity.
main.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/imagenext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="#drawable/next" />
</RelativeLayout>
After read all Answers I came up with elegant way:
public class MyActivity extends ActionBarActivity {
Fragment fragment ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
fragment = fm.findFragmentByTag("myFragmentTag");
if (fragment == null) {
FragmentTransaction ft = fm.beginTransaction();
fragment =new MyFragment();
ft.add(android.R.id.content,fragment,"myFragmentTag");
ft.commit();
}
}
basically you don't need to add a frameLayout as container of your fragment instead you can add straight the fragment into the android root View container
IMPORTANT: don't use replace fragment as most of the approach shown here, unless you don't mind to lose fragment variable instance state during onrecreation process.
For attaching fragment to an activity programmatically in Kotlin, you can look at the following code:
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// create fragment instance
val fragment : FragmentName = FragmentName.newInstance()
// for passing data to fragment
val bundle = Bundle()
bundle.putString("data_to_be_passed", DATA)
fragment.arguments = bundle
// check is important to prevent activity from attaching the fragment if already its attached
if (savedInstanceState == null) {
supportFragmentManager
.beginTransaction()
.add(R.id.fragment_container, fragment, "fragment_name")
.commit()
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".ui.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
FragmentName.kt
class FragmentName : Fragment() {
companion object {
fun newInstance() = FragmentName()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// receiving the data passed from activity here
val data = arguments!!.getString("data_to_be_passed")
return view
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
}
}
If you are familiar with Extensions in Kotlin then you can even better this code by following this article.
public abstract class SingleFragmentActivity extends Activity {
public static final String FRAGMENT_TAG = "single";
private Fragment fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
fragment = onCreateFragment();
getFragmentManager().beginTransaction()
.add(android.R.id.content, fragment, FRAGMENT_TAG)
.commit();
} else {
fragment = getFragmentManager().findFragmentByTag(FRAGMENT_TAG);
}
}
public abstract Fragment onCreateFragment();
public Fragment getFragment() {
return fragment;
}
}
use
public class ViewCatalogItemActivity extends SingleFragmentActivity {
#Override
public Fragment onCreateFragment() {
return new FragmentWorkShops();
}
}
For API level 17 or higher, View.generateViewId() will solve this problem. The utility method provides a unique id that is not used in build time.
This may help you
Defining a Fragment
create xml file for fragment view fragment_abc.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
create fragment ABCFragment.java
import androidx.fragment.app.Fragment;
public class FooFragment extends Fragment {
// The onCreateView method is called when Fragment should create its View object hierarchy,
// either dynamically or via XML layout inflation.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle
savedInstanceState) {
// Defines the xml file for the fragment
return inflater.inflate(R.layout.fragment_abc, parent, false);
}
// This event is triggered soon after onCreateView().
// Any view setup should occur here. E.g., view lookups and attaching view listeners.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Setup any handles to view objects here
// EditText etFoo = (EditText) view.findViewById(R.id.etFoo);
}
}
Add frameLayout in your activity
<FrameLayout
android:id="#+id/your_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent">
now in activity, add following method
protected void setFragment() {
// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.fragment_container, new ABCFragment());
// or ft.add(R.id.your_placeholder, new ABCFragment());
// Complete the changes added above
ft.commit();
}
reference : https://guides.codepath.com/android/creating-and-using-fragments#defining-a-fragment

Categories

Resources