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
Fragment inner class should be static的更多相关文章
- Android Fragment 你应该知道的一切
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42628537,本文出自:[张鸿洋的博客] 很久以前写过两篇Fragment的介绍 ...
- Android之Fragment学习总结(1)
对于Fragment的学习: 近日初步学习了Fragment这以特殊的组件,其依托与一个Activity,与Activity的生命周期息息相关,为其设置的视图只有当其关联到一个Activity才会起效 ...
- Android 开发 之 Fragment 详解
本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...
- Using newInstance() to Instantiate a Fragment(转)
I recently came across an interesting question on StackOverflow regarding Fragment instantiation: Wh ...
- Fragment 总结
本博客代码地址 : -- 单一 Fragment 示例 : https://github.com/han1202012/Octopus-Fragement.git -- 可复用的 Fragment 示 ...
- Android中FragmentPagerAdapter对Fragment的缓存(二)
上一篇我们谈到了,当应用程序恢复时,由于FragmentPagerAdapter对Fragment进行了缓存的读取,导致其并未使用在Activity中新创建的Fragment实例.今天我们来看如何解决 ...
- Android UI开发详解之Fragment
Fragment是Android自从3.0之后新加入的一个组件,我相信很多人都已经听说过这个组件了,但这个组件到底是个什么,如何去使用他呢,且听我讲来. 以下部分资料来自官网(官网才是王道,其他都是浮 ...
- Android Fragment学习(一)
说明 Fragment是在Android3.0(即API 11)才出现的,如果在之前的版本要使用,需要添加support库. Fragment可以认为是Actvity模块化的组件,可以很方便地被添加, ...
- Android - 封装Fragment不依赖于Activity
封装Fragment不依赖于Activity 本文地址:http://blog.csdn.net/caroline_wendy Fragment直接托管activity的intent会破坏Fragme ...
随机推荐
- 【原则】常用windows开发 客户端工具 收集
1. Navicat Premium 推荐: mysql客户端, postgreSQL 客户端, Sqlite客户端 2. robomongo 推荐:mongoDB客户端
- html+css-水平居中-不定款块状元素方法(二)
来源:http://www.imooc.com/code/6364 除了上一节讲到的插入table标签,可以使不定宽块状元素水平居中之外,本节介绍第2种实现这种效果的方法,改变元素的display类型 ...
- .NET DLL 保护措施详解(三)最终效果
针对.NET DLL 保护措施详解所述思路完成最终的实现,以下为程序包下载地址 下载 注意: 运行环境为.net4.0,需要安装VS2015 C++可发行组件包vc_redist.x86.exe.然后 ...
- Js基础知识-入门
创建脚本块 <script language=”JavaScript”> JavaScript code goes here </script> 隐藏脚本代码 <scri ...
- Java之姐妹素数
所谓素数就是指相邻两个奇数均为素数, 判断一个数是否为素数的基本方法是:(以n=5为例) package com.cdp.SuShu; public class sushujisuan { publi ...
- ubuntu安装 ibus-google输入法
1.$sudo apt-get install ibus-googlepinyin //ibus 融合了许多种输入法,google便是一种,此步就是下载安装ibus-google拼音输入法. ...
- js jquery jquery.wordexport.js 实现导出word
由于工作需要,将一个页面导出word文档,主要是简历!经过百度搜索之后,没找到结果,无奈之下只能求助Google,意外发现jquery一款插件可以实现这个功能!而且效果还算可以! 基本可以实现想要的功 ...
- Android开发之切换activity动画overridePendingTransition
原文地址:http://blog.sina.com.cn/s/blog_706c449f01011s3v.html overridePendingTransition 在startActivity() ...
- 10款经典的web前端特效的预览及源码
1.CSS3响应式导航菜单 今天我给大家介绍一下如何使用纯CSS来实现的一个响应式导航菜单,我们使用的是HTML5+CSS3技术,当浏览器窗口变小或者使用手机浏览器访问的时候,原本横条菜单会收缩成一个 ...
- 推荐7款新鲜出炉的HTML5/CSS3应用
1.HTML5/CSS3发光文字可自定义文字色彩效果很赞 要分享的这款HTML5/CSS3文字效果很帅,鼠标滑过文字时,文字会出现发光的特效,并且我们可以自定义文字和颜色. 在线演示 源码下载 2.H ...