在Android 4.4中必须使用片段

时间:2014-04-30 10:01:29

标签: android android-fragments

根据Android 4.4开发者指南,我们必须在每个活动中使用Fragment。如果我们使内部片段类非静态,那么当它从背景到前景时Android会崩溃我们的应用程序,所以我们需要使片段类静态。在这种情况下,片段类中使用的所有变量和类(片段类变量除外)都应该是静态的。

我认为这将消耗更多的VM内存,应用程序将不稳定。任何人都可以建议我是对的吗?或者建议是否有其他方法可以解决这个问题。

以下是代码片段。

public class MainActivity extends ActionBarActivity {

    // Video variable
    MediaRecorder recorder; 
    // Networking variables
    public static String SERVERIP="";
    public static final int SERVERPORT = 6775;
    private Handler handler = new Handler();
    private ServerSocket serverSocket;
    public static SurfaceHolder mHolder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }
/**
     * A placeholder fragment containing a simple view.
     */


    public static class PlaceholderFragment extends Fragment {

        // User Interface Elements
        VideoView mView;
        TextView connectionStatus;



        /** Called when the activity is first created. */

        public PlaceholderFragment() {
        }
         NotificationManager notificationManager;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onViewCreated(view, savedInstanceState);
            Button btn = (Button)view.findViewById(R.id.notifybtnID);
            btn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                }
            });

              // Define UI elements
            mView = (VideoView) view.findViewById(R.id.video_preview);
          //  connectionStatus = (TextView) findViewById(R.id.connection_status_textview);
            mHolder = mView.getHolder();
           // mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            SERVERIP = "192.168.1.126";
            // Run new thread to handle socket communications
            Thread sendVideo = new Thread(new SendVideoThread());
            sendVideo.start();
        }
public static class SendVideoThread implements Runnable{
        public void run(){
            // From Server.java
            try {
                if(SERVERIP!=null){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                         //   connectionStatus.setText("Listening on IP: " + SERVERIP);
                        }
                    });
}

0 个答案:

没有答案