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 ...
随机推荐
- Jquery和JS删除提示
Jquery <script> $(function () { $('.deletes').click(function () { //提示 if (!confirm('确定呀删除吗?') ...
- DWZ与KindEditor编辑器的整合
DWZ自带的编辑器是xheditor,可能很多人用不习惯.就像我,习惯用kindeditor了.现在就来说说如何整合dwz和kindeditor. 一.打开DWZ的中的dwz.ui.js,进行修改. ...
- jQuery插件的编写和使用 <思维导图>
以下是jQuery插件的编写和使用的思维导图,全屏观看,请点击:jQuery插件的编写和使用
- OSPF系列
实验一.点对点链路上的OSPF 拓扑图 1. 首先配置好路由器R1接口地址和回环地址 2. 配置路由器R2的接口地址和回环地址 3. 配置路由器R3的接口地址和环回地址 4. 配置R1的OSPF协议 ...
- SQL Server 安装程序失败 不能在控件上调用 Invoke 或 BeginInvoke
出现这种问题的原因是权限问题,怎么处理呢,使用管理员运行 如果这种方法不行,比如我的就不可以,点击右键 对各个权限对象重新添加完全控制权限. 我的电脑的情况是安装sql2010,然后安装sql管理工具 ...
- JSON字符串和js对象转换
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON js数据类型: 字符串.数字 ...
- struts2,hibernate4,spring3配置时问题汇总及解决办法
文章转载于wanglihu的博客,原文链接http://wanglihu.iteye.com/blog/1897718 1.java.lang.NoClassDefFoundError: org/ob ...
- LitJSON使用
地址:http://lbv.github.io/litjson/docs/quickstart.html LitJSON Quickstart Guide Introduction Quick Sta ...
- 【Qt】Qt之重启应用程序【转】
简介 今天分享的内容有些意思-如何重启一个应用程序.其实,有时候这是一个很重要的功能点,而且很人性化.易用性很好. 例如:切换用户.当某个用户登录成功之后,需要切换到其它账号,那么这时,你就知道它的重 ...
- AngularJS(13)-包含
AngularJS 包含 使用 AngularJS, 你可以使用 ng-include 指令来包含 HTML 内容: 实例 <body> <div class="conta ...