Fragment inner class should be static

package com.example.fragmenttest;



import android.annotation.SuppressLint;

import android.app.Activity;

import android.app.Fragment;

import android.app.FragmentTransaction;

import android.content.res.Configuration;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;



public class WrongFragmentUsageActivity extends Activity

{

private String mActivityOrientation="";

private int mButtonClicks=0;

private TextView mClickTextView;





private static final String WRONG_FRAGMENT_TAG = "WrongFragment" ;



@Override

protected void onCreate(Bundle savedInstanceState)

{

    super.onCreate(savedInstanceState);

    int orientation = getResources().getConfiguration().orientation;

    if (orientation == Configuration.ORIENTATION_LANDSCAPE)

    {

        mActivityOrientation = "Landscape";

    }

    else if (orientation == Configuration.ORIENTATION_PORTRAIT)

    {

        mActivityOrientation = "Portrait";

    }



    setContentView(R.layout.activity_wrong_fragement_usage);

    mClickTextView = (TextView) findViewById(R.id.clicksText);

    updateClickTextView();

    TextView orientationtextView = (TextView) findViewById(R.id.orientationText);

    orientationtextView.setText("Activity orientation is: " + mActivityOrientation);



    Fragment wrongFragment = (WrongFragment) getFragmentManager().findFragmentByTag(WRONG_FRAGMENT_TAG);

    if (wrongFragment == null)

    {

        wrongFragment = new WrongFragment();

        FragmentTransaction ft = getFragmentManager().beginTransaction();

        ft.add(R.id.mainView, wrongFragment, WRONG_FRAGMENT_TAG);

        ft.commit();

        wrongFragment.setRetainInstance(true); // <-- this is important - otherwise the fragment manager will crash when readding the fragment

    }

}



private void updateClickTextView()

{

    mClickTextView.setText("The buttons have been pressed " + mButtonClicks + " times");

}



private String getActivityOrientationString()

{

    return mActivityOrientation;

}





@SuppressLint("ValidFragment")

public class WrongFragment extends Fragment

{





    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

    {

        LinearLayout result = new LinearLayout(WrongFragmentUsageActivity.this);

        result.setOrientation(LinearLayout.VERTICAL);

        Button b = new Button(WrongFragmentUsageActivity.this);

        b.setText("WrongFragmentButton");

        result.addView(b);

        b.setOnClickListener(new View.OnClickListener()

        {

            @Override

            public void onClick(View v)

            {

                buttonPressed();

            }

        });

        TextView orientationText = new TextView(WrongFragmentUsageActivity.this);

        orientationText.setText("WrongFragment Activities Orientation: " + getActivityOrientationString());

        result.addView(orientationText);

        return result;

    }

}



public static class CorrectFragment extends Fragment

{

    private WrongFragmentUsageActivity mActivity;





    @Override

    public void onAttach(Activity activity)

    {

        if (activity instanceof WrongFragmentUsageActivity)

        {

            mActivity = (WrongFragmentUsageActivity) activity;

        }

        super.onAttach(activity);

    }



    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

    {

        LinearLayout result = new LinearLayout(mActivity);

        result.setOrientation(LinearLayout.VERTICAL);

        Button b = new Button(mActivity);

        b.setText("CorrectFragmentButton");

        result.addView(b);

        b.setOnClickListener(new View.OnClickListener()

        {

            @Override

            public void onClick(View v)

            {

                mActivity.buttonPressed();

            }

        });

        TextView orientationText = new TextView(mActivity);

        orientationText.setText("CorrectFragment Activities Orientation: " + mActivity.getActivityOrientationString());

        result.addView(orientationText);

        return result;

    }

}



public void buttonPressed()

{

    mButtonClicks++;

    updateClickTextView();

}



}

http://stackoverflow.com/questions/15571010/fragment-inner-class-should-be-static

你可能感兴趣的:(inner class)