Android基础之再回顾
来源:互联网 发布:广联达计价软件 编辑:程序博客网 时间:2024/05/22 00:12
找个工作好难啊,我就想找个实习,挣点学费有这么难吗?面试公司问我源码,几个月以前学过的东西,哪还记得啊。。。让我看看代码我还能写出来,直接问我那记得起来。都是程序员,给老板打工的,何必这么为难呢,你直接给我个任务,我能完成,你多多考虑下,完不成,我自己走人,再说我只是想找实习啊。唉,只是发发牢骚,不能怪公司,只能说自己没学好,还是回顾一下吧。
1、Activity的生命周期
我们都知道activity的生命周期有七个,并且知道它们的作用,但是当一个activity中点击一个按钮启动另一个activity,并且在第二个activity按下back键时,这两个activity的生命周期是怎么运行的?
设第一个activity为A,第二个activity为B,则以下为他们的执行顺序:
ActivityA: ------------------onCreateActivityA: ------------------onStartActivityA: ------------------onResumeActivityA: ------------------onPauseActivityB: ------------------onCreateActivityB: ------------------onStartActivityB: ------------------onResumeActivityA: ------------------onStopActivityB: ------------------onPauseActivityA: ------------------onRestartActivityA: ------------------onStartActivityA: ------------------onResumeActivityB: ------------------onStopActivityB: ------------------onDestroy
2、Service的启动方式
现在有俩个activity,一个服务。
那么以下几个过程是怎么样的?
先看代码。
第一个activity
public class ActivityA extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.helloword); Intent intent = new Intent(ActivityA.this, TheService.class); startService(intent); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ActivityA.this,ActivityB.class); startActivity(intent); } });// Log.d("ActivityA","------------------onCreate"); } @Override protected void onStart() { super.onStart();// Log.d("ActivityA","------------------onStart"); } @Override protected void onPause() { super.onPause();// Log.d("ActivityA","------------------onPause"); } @Override protected void onStop() { super.onStop();// Log.d("ActivityA","------------------onStop"); } @Override protected void onDestroy() { super.onDestroy();// Log.d("ActivityA","------------------onDestroy"); } @Override protected void onResume() { super.onResume();// Log.d("ActivityA","------------------onResume"); } @Override protected void onRestart() { super.onRestart();// Log.d("ActivityA","------------------onRestart"); }}
第二个activity
public class ActivityB extends AppCompatActivity { ServiceConnection sc = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { TheService.MyServiceBinder myServiceBinder = (TheService.MyServiceBinder) service; TheService theService = myServiceBinder.getService(); } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); TextView textView = (TextView) findViewById(R.id.thissssss); textView.setText("fffffffffffffffffff"); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ActivityB.this,TheService.class); stopService(intent); } });// Log.d("ActivityB","------------------onCreate"); Intent intent = new Intent(ActivityB.this, TheService.class); bindService(intent, sc,BIND_AUTO_CREATE); } @Override protected void onStart() { super.onStart();// Log.d("ActivityB","------------------onStart"); } @Override protected void onPause() { super.onPause();// Log.d("ActivityB","------------------onPause"); } @Override protected void onStop() { super.onStop();// Log.d("ActivityB","------------------onStop"); } @Override protected void onDestroy() { super.onDestroy();// Log.d("ActivityB","------------------onDestroy"); Intent intent = new Intent(ActivityB.this, TheService.class); unbindService(sc); } @Override protected void onResume() { super.onResume();// Log.d("ActivityB","------------------onResume"); } @Override protected void onRestart() { super.onRestart();// Log.d("ActivityB","------------------onRestart"); }}
服务
public class TheService extends Service{ private MyServiceBinder myServiceBinder; @Override public void onCreate() { super.onCreate(); Log.e("TheService","--------------onCreate"); myServiceBinder = new MyServiceBinder(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("TheService","--------------onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e("TheService","--------------onDestroy"); super.onDestroy(); } @Nullable @Override public IBinder onBind(Intent intent) { Log.e("TheService","--------------onBind"); return myServiceBinder; } @Override public boolean onUnbind(Intent intent) { Log.e("TheService","--------------onUnbind"); return super.onUnbind(intent); } public class MyServiceBinder extends Binder { public TheService getService(){ return TheService.this; } }}
- 第一个方式:直接先打开第一个activity,然后打开第二个activity,并且在第二个activity打开后直接按下back键,返回到第一个界面后再按下back键,退出应用。
打开Aactivity:
TheService: --------------onCreateTheService: --------------onStartCommand
打开BActivity
TheService: --------------onBind
按下back键
TheService: --------------onUnbind
退出应用程序什么都不会打印。
看应用管理器,服务还在运行,强制结束后,打印:
TheService: --------------onDestroy
服务结束
- 第二个方式,打开应用,然后直接打开第二个界面,并且点击按钮调用stopService,然后按下back键,最后结束应用。
打开应用:
TheService: --------------onCreateTheService: --------------onStartCommand
打开第二个界面
TheService: --------------onBind
点击textview什么都不会发生,按下back键后。
TheService: --------------onUnbindTheService: --------------onDestroy
服务已经结束
- 第三个方式,按照第一种方式操作直到退出应用,不去应用管理器关服务,而是直接打开应用。
打开Aactivity:
TheService: --------------onCreateTheService: --------------onStartCommand
打开BActivity
TheService: --------------onBind
按下back键
TheService: --------------onUnbind
退出应用程序没有打印。
重启应用
TheService: --------------onStartCommand
3、Fragment的创建
我所用到的有三种方式:
1、第一种
首先定义一个类:
import android.app.Activity;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.example.houjie.studyapk.R;public class LifecycleFragment extends Fragment{ private final String TAG ="LifecycleFragment"; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { Log.i(TAG,"Fragment onCreateView"); return inflater.inflate(R.layout.layout_only_textview,null); }}
在这儿返回一个只有textview的LinearLayout
xml中直接定义Fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.houjie.studyapk.studyfragment.fragment.LifecycleActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除Fragment" android:id="@+id/FragmentBtnClear" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="创建Fragment" android:id="@+id/FragmentBtnCreate" /> <fragment android:layout_width="wrap_content" android:layout_height="wrap_content" class="com.example.houjie.studyapk.studyfragment.fragment.LifecycleFragment" android:id="@+id/Fragmentfragment"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="nihao" /> </fragment></LinearLayout>
定义fragment,用class属性声明,这里必须要包的绝对路径
声明好了就是使用
public class LifecycleActivity extends AppCompatActivity { private final String TAG ="LifecycleActivity"; private FragmentManager fragmentManager; private Button button1; private Button button2; private LifecycleFragment lifecycleFragment; private int page; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment_lifecycle); Log.i(TAG,"Activity onCreate"); button1= (Button) findViewById(R.id.FragmentBtnClear); button2= (Button) findViewById(R.id.FragmentBtnCreate); fragmentManager=getSupportFragmentManager(); lifecycleFragment =(LifecycleFragment)fragmentManager.findFragmentById(R.id.Fragmentfragment); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.remove(lifecycleFragment); transaction.commit(); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); }}
在这儿我试着移除,但是无法移除。
“不可以的。要换Fragment只能通过框架布局来操作。关于灵活动态变化UI一般都使用Fragment的。如果你先remove掉,那就直接用框架布局来操作。”这是我百度到的一句话。
2、第二种(用框架布局)
第一个Fragment
public class FragmentOne extends Fragment { private ImageView imageView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_only_imageview,null); imageView = (ImageView) view.findViewById(R.id.OnlyImageView); imageView.setImageResource(R.mipmap.picture27); return view; }}
第二个Fragment
public class FragmentTwo extends Fragment{ private ListView mListView; private List<String> mList; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view =inflater.inflate(R.layout.layout_only_listview,null); mListView = (ListView) view.findViewById(R.id.OnlyListView); mList =new ArrayList<>(); mList.add("你好"); mList.add("我好"); mList.add("大家好"); mList.add("你好"); mList.add("我好"); mList.add("大家好"); mList.add("你好"); mList.add("我好"); mList.add("大家好"); mList.add("你好"); mList.add("我好"); mList.add("大家好"); mList.add("你好"); mList.add("我好"); mList.add("大家好"); mList.add("你好"); mList.add("我好"); mList.add("大家好"); ArrayAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_expandable_list_item_1,mList); mListView.setAdapter(adapter); return view; }}
再来看看activity的布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".studyfragment.fragment.FragmentActivityTest"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/fragmentlayout" android:layout_weight="1" > </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/FragmentLinearLayout" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一页" android:id="@+id/FragmentBtnUp" android:layout_weight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一页" android:id="@+id/FragmentBtnNext" android:layout_weight="1" /> </LinearLayout></LinearLayout>
这里只有一个FrameLayout和两个button
再来看看activity
public class FragmentActivityTest extends AppCompatActivity { private Button button1,button2; private FragmentManager manager; private FragmentOne fragmentOne; private FragmentTwo fragmentTwo; private FrameLayout frameLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment_two); manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); fragmentTwo = new FragmentTwo(); fragmentOne = new FragmentOne(); transaction.add(R.id.fragmentlayout,fragmentTwo).commit(); button1= (Button) findViewById(R.id.FragmentBtnUp); button2= (Button) findViewById(R.id.FragmentBtnNext); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentTransaction transaction = manager.beginTransaction(); //必须写在此处,因为它只执行一次就会销毁 transaction.replace(R.id.fragmentlayout,fragmentTwo); transaction.commit(); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentTransaction transaction = manager.beginTransaction(); //必须写在此处,因为它只执行一次就会销毁 transaction.replace(R.id.fragmentlayout,fragmentOne); transaction.commit(); } }); }}
这样就可以实现在FrameLayout中替换Fragment
看一下效果
还有一种是用transaction 的add、show、hide三个方法来显示。
3、第三种
用viewPager+Fragment组合来实现
先看几个类
public class FragmentPagerFour extends Fragment { private ImageView imageView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_only_imageview,null); imageView = (ImageView) view.findViewById(R.id.OnlyImageView); imageView.setImageResource(R.mipmap.a3); return view; }}
public class FragmentPagerOne extends Fragment { private ImageView imageView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_only_imageview,null); imageView = (ImageView) view.findViewById(R.id.OnlyImageView); imageView.setImageResource(R.mipmap.ic_launcher); return view; }}
public class FragmentPagerThree extends Fragment { private ImageView imageView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_only_imageview,null); imageView = (ImageView) view.findViewById(R.id.OnlyImageView); imageView.setImageResource(R.mipmap.a2); return view; }}
public class FragmentPagerTwo extends Fragment{ private ImageView imageView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_only_imageview,null); imageView = (ImageView) view.findViewById(R.id.OnlyImageView); imageView.setImageResource(R.mipmap.a1); return view; }}
还有一个适配器:
public class TestFragmentPagerAdapter extends FragmentStatePagerAdapter { private List<Fragment> list; private Context intent; public TestFragmentPagerAdapter(FragmentManager fm, List<Fragment> list, Context intent) { super(fm); this.list = list; this.intent = intent; } @Override public int getCount() { return list.size(); } @Override public Fragment getItem(int position) { return list.get(position); }}
接下来是xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ViewFragmentPagerTest" android:layout_weight="1" > </android.support.v4.view.ViewPager> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/FragmentPagerLinearLayout" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/FragmentPagerBtn1" android:layout_weight="1" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/FragmentPagerBtn2" android:layout_weight="1" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/FragmentPagerBtn3" android:layout_weight="1" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/FragmentPagerBtn4" android:layout_weight="1" /> </LinearLayout></LinearLayout>
这里没有Fragment也没有FrameLayout,只有viewPager
接下来是activity
public class ActivityFragmentPager extends AppCompatActivity implements View.OnClickListener,ViewPager.OnPageChangeListener{ private ViewPager mViewPager; private List<Fragment> list; private ImageButton button1,button2,button3,button4; private FragmentManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment_pager); mViewPager =(ViewPager)findViewById(R.id.ViewFragmentPagerTest); list = new ArrayList<>(); list.add(new FragmentPagerOne()); list.add(new FragmentPagerTwo()); list.add(new FragmentPagerThree()); list.add(new FragmentPagerFour()); manager = getSupportFragmentManager(); TestFragmentPagerAdapter testFragmentPagerAdapter =new TestFragmentPagerAdapter(manager,list,this); mViewPager.setOnPageChangeListener(this); mViewPager.setAdapter(testFragmentPagerAdapter); init(); } private void init() { button1= (ImageButton) findViewById(R.id.FragmentPagerBtn1); button1.setOnClickListener(this); button2= (ImageButton) findViewById(R.id.FragmentPagerBtn2); button2.setOnClickListener(this); button3= (ImageButton) findViewById(R.id.FragmentPagerBtn3); button3.setOnClickListener(this); button4 = (ImageButton) findViewById(R.id.FragmentPagerBtn4); button4.setOnClickListener(this); button1.setImageResource(R.mipmap.a); button2.setImageResource(R.mipmap.bb); button3.setImageResource(R.mipmap.cc); button4.setImageResource(R.mipmap.dd); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.FragmentPagerBtn1:Fragment1();break; case R.id.FragmentPagerBtn2:Fragment2();break; case R.id.FragmentPagerBtn3:Fragment3();break; case R.id.FragmentPagerBtn4:Fragment4();break; } } private void Fragment1() { mViewPager.setCurrentItem(0,true); } private void Fragment2() { mViewPager.setCurrentItem(1,true); } private void Fragment3() { mViewPager.setCurrentItem(2,true); } private void Fragment4() { mViewPager.setCurrentItem(3,true); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {// change(position); } @Override public void onPageSelected(int position) { change(position); } @Override public void onPageScrollStateChanged(int state) {// change(state); } private void change(int pp){ Toast.makeText(this,"ssdfsdf",Toast.LENGTH_SHORT).show(); button1.setImageResource(R.mipmap.aa); button2.setImageResource(R.mipmap.bb); button3.setImageResource(R.mipmap.cc); button4.setImageResource(R.mipmap.dd); switch (pp){ case 0: button1.setImageResource(R.mipmap.a);break; case 1:button2.setImageResource(R.mipmap.b);break; case 2:button3.setImageResource(R.mipmap.c);;break; case 3:button4.setImageResource(R.mipmap.d);;break; } }}
我们看一下效果
这样就很容易地实现了Fragment的替换。
- Android基础之再回顾
- Android之service一些基础回顾
- Android 回顾Service之Service基础使用
- 基础回顾之DateFormater
- Android基础回顾笔记
- Android基础回顾--BroadcastReceiver
- Android基础回顾--Activity
- Android回顾之布局
- Android回顾之短信
- Java回顾之Spring基础
- Java回顾之Spring基础
- Java基础回顾之集合
- 基础再回顾
- Java 基础再回顾
- Android回顾之四大组件
- Android回顾之XML解析
- Android回顾之JSON解析
- Android回顾之数据存储
- httpclient_get_post
- Spring对JNDI的支持的配置实例(JNDI简介)
- 欢迎使用CSDN-markdown编辑器
- CDH安装
- http摘要认证
- Android基础之再回顾
- Android之批量加载图片OOM问题解决方案
- Android alertdialog的自定义添加按钮和添加点击
- Spinner
- The serializable class Myplayer does not declare a static final serialVersionUID field of type long
- BZOJ2083: [Poi2010]Intelligence test
- Android Studio库工程上传maven
- mininet安装与使用,wireshark抓包器, iperf网络性能检测
- 保护模式及其编程——实模式与保护模式的切换