add initial support for fragments

Fragments are not added to the View hierarchy yet. Only the lifecycle
callbacks are implemented
This commit is contained in:
Julian Winkler
2023-08-22 13:29:44 +02:00
parent 83cc2e5991
commit faf4a3281e
4 changed files with 70 additions and 3 deletions

View File

@@ -23,6 +23,8 @@ import java.io.InputStream;
import java.io.StringReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
@@ -36,6 +38,7 @@ public class Activity extends Context {
private int pendingRequestCode;
private int pendingResultCode;
private Intent pendingData;
List<Fragment> fragments = new ArrayList<>();
/**
* Helper function to be called from native code to construct main activity
@@ -110,6 +113,10 @@ public class Activity extends Context {
System.out.println("- onCreate - yay!");
new ViewGroup(this).setId(R.id.content);
for (Fragment fragment : fragments) {
fragment.onCreate(savedInstanceState);
}
return;
}
@@ -118,6 +125,10 @@ public class Activity extends Context {
if (window.contentView != null)
window.setContentView(window.contentView);
for (Fragment fragment : fragments) {
fragment.onStart();
}
return;
}
@@ -134,24 +145,40 @@ public class Activity extends Context {
pendingData = null;
}
for (Fragment fragment : fragments) {
fragment.onResume();
}
return;
}
protected void onPause() {
System.out.println("- onPause - yay!");
for (Fragment fragment : fragments) {
fragment.onPause();
}
return;
}
protected void onStop() {
System.out.println("- onStop - yay!");
for (Fragment fragment : fragments) {
fragment.onStop();
}
return;
}
protected void onDestroy() {
System.out.println("- onDestroy - yay!");
for (Fragment fragment : fragments) {
fragment.onDestroy();
}
return;
}
@@ -291,7 +318,7 @@ public class Activity extends Context {
}
public FragmentManager getFragmentManager() {
return new FragmentManager();
return new FragmentManager(this);
}
public LayoutInflater getLayoutInflater() {

View File

@@ -1,5 +1,31 @@
package android.app;
import android.os.Bundle;
public class Fragment {
Activity activity;
public void onCreate(Bundle savedInstanceState) {
}
public void onStart() {
}
public void onResume() {
}
public void onPause() {
}
public void onStop() {
}
public void onDestroy() {
}
public Activity getActivity() {
return activity;
}
}

View File

@@ -2,12 +2,18 @@ package android.app;
public class FragmentManager {
private Activity activity;
public FragmentManager(Activity activity) {
this.activity = activity;
}
public Fragment findFragmentByTag(String tag) {
return null;
}
public FragmentTransaction beginTransaction() {
return new FragmentTransaction();
return new FragmentTransaction(activity);
}
public boolean executePendingTransactions() {

View File

@@ -2,7 +2,15 @@ package android.app;
public class FragmentTransaction {
private Activity activity;
public FragmentTransaction(Activity activity) {
this.activity = activity;
}
public FragmentTransaction add(Fragment fragment, String string) {
fragment.activity = activity;
activity.fragments.add(fragment);
return this;
}