我在LoginActivity中使用两个片段登录片段和寄存器片段
在活动加载时,调用登录片段并且没有问题 当我尝试将loginFragment替换为registerFragment时,我遇到了以下崩溃:
Process: symphony.vnet.acube.com.symphonyupdated, PID: 28073
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2087)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632)
at com.vnet.acube.com.vnetinc.acitivity.helper.AcitivityExtensionsKt.addFragment(AcitivityExtensions.kt:16)
at com.vnet.acube.com.vnetinc.acitivity.loginRegister.LoginActivity.openRegisterFragment(LoginActivity.kt:38)
at com.vnet.acube.com.vnetinc.acitivity.loginRegister.LoginFragment.onClick(LoginFragment.kt:27)
at android.view.View.performClick(View.java:5265)
at android.view.View$PerformClick.run(View.java:21534)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5737)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
这是我的代码:
登录活动:
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
if(savedInstanceState==null)
{
addFragment(LoginFragment.newInstance(),R.id.details_fragment)
}
}
public fun openLoginFragment() {
replaceFragment(LoginFragment.newInstance(),R.id.details_fragment)
}
override fun onBackPressed()
{
}
public fun openRegisterFragment(){
replaceFragment(RegisterFragment.newInstance(),R.id.details_fragment)
}
}
登录片段:
class LoginFragment : Fragment(),View.OnClickListener {
override fun onClick(p0: View?) {
if(p0!!.id == R.id.tvNewUser)
{
var loginActivity:LoginActivity = LoginActivity()
loginActivity.openRegisterFragment()
}
}
companion object {
fun newInstance():LoginFragment{
return LoginFragment()
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
var view:View = inflater.inflate(R.layout.fragment_login, container, false)
return view
}
}
注册片段
class RegisterFragment : Fragment() {
companion object {
fun newInstance(): RegisterFragment
{
return RegisterFragment()
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_register, container, false)
}
}
能力扩展
inline fun FragmentManager.inTransaction(func: FragmentTransaction.()->FragmentTransaction)
{
beginTransaction().func().commit()
}
fun AppCompatActivity.addFragment(fragment: Fragment, frameId:Int)
{
supportFragmentManager.inTransaction { add(frameId,fragment) }
}
fun AppCompatActivity.replaceFragment(fragment: Fragment,frameId: Int)
{
supportFragmentManager.inTransaction { replace(frameId,fragment) }
}