80,351
社区成员
发帖
与我相关
我的任务
分享
import Fragments.FW_EntryLobby;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity {
public static MainActivity mainstance;
ViewGroup vg_mainframe;
FragmentActivity fa;//replaces FragmentManager
FragmentManager fm;
FragmentTransaction ft;//FragmentTransaction事务对象
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mainstance=this;
init_fragments();
loadWhole();
init_entrylobby();
}
private void init_fragments()//fragment is deprecated since API28
{
//https://stackoverflow.com/questions/51737667/since-the-android-getfragmentmanager-api-is-deprecated-is-there-any-alternati
fa=new FragmentActivity();
fm = fa.getSupportFragmentManager();
ft = fm.beginTransaction();
}
public void loadWhole()
{
vg_mainframe= (ViewGroup) MainActivity.mainstance.findViewById(R.id.fl_am);
}
FW_EntryLobby flayout_entrylobby;
public void init_entrylobby()
{
if(flayout_entrylobby==null)
flayout_entrylobby = new FW_EntryLobby();
ft.replace(R.id.fl_am,flayout_entrylobby);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();//用commit()就报错啊
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl_am">
</FrameLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout class="Fragments.FW_EntryLobby"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
>
<Button
android:id="@+id/b_entrylobby_fetch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0px"
android:layout_marginTop="0px"
android:text="enter" />
</RelativeLayout>
package Fragments;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.cd2.R;
public class FW_EntryLobby extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.lf_entrylobby, null);
return rootView;
}
}