Tuesday, 24 December 2019

Load fragment with layout from another APK

I have two APKs. First APK is a main APK which loads second APK.

MainActivity.java (first APK): Object mainFragment was loaded by DexClassLoader early

setContentView(R.layout.activity_main);

LinearLayout fragContainer = findViewById(R.id.main_fragment);

LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(View.generateViewId());

getSupportFragmentManager().beginTransaction().add(ll.getId(), mainFragment, "mainFragment").commit();

fragContainer.addView(ll);

activity_main.xml (first APK):

<?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="horizontal" >
    <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/main_fragment"
            android:orientation="vertical"
            />

</LinearLayout>

MainFragment.java (second APK):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main, viewGroup, false);

    view.findViewById(R.id.emailSignInButton).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

        }
    });

    return view;
}

fragment_main.xml (second APK):

<?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">
    <Button
            android:text="@string/sign_in"
            android:layout_width="88dp"
            android:layout_height="wrap_content"
            android:id="@+id/emailSignInButton"

            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp" android:layout_marginTop="8dp"
            tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

I don't understand, why in the line

View view = inflater.inflate(R.layout.fragment_main, viewGroup, false);

view is always NULL. Do I need to load R.layout.fragment_main into memory like mainFragment?



from Load fragment with layout from another APK

No comments:

Post a Comment