电话的方向和片段翻译错误。
我有一个活动和一些片段。第一个片段立即带有徽标,经过活动,然后3秒钟后,第二个片段翻译过来。问题是,如果我改变手机的方向,第一个片段会以与启动应用程序相同的延迟和相同的行为重新出现。
MainActivity:
package com.mainpackage.pinbook;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.mainpackage.pinbook.com.autentification.LoginFragment;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container , new MainScreen());
fragmentTransaction.commit();
}
}
主屏幕:
package com.mainpackage.pinbook;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mainpackage.pinbook.com.autentification.*;
public class MainScreen extends Fragment{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_screen , container , false);
}
private TextView entry_text;
public static final String TAG = MainScreen.class.getSimpleName();
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
slide();
}
}, 3000);
}
private void slide(){
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(0,R.anim.slide_in_left);
fragmentTransaction.replace(R.id.main_container , new LoginFragment());
fragmentTransaction.commit();
}
@Override
public void onStop() {
super.onStop();
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
@Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
}
第二个片段可以为空白。
我想要的只是在更改手机方向时保留在当前片段上
答案 0 :(得分:1)
设备旋转时,props
被再次调用。
像这样将片段交易包裹在onCreate()
中:
onCreate()
这将防止旋转时将第一个片段放回顶部。