赞
踩
代表手机或平板上的一屏

在onResume执行后,Activity就是可见的了
可以通过下面代码查看生命周期
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivityTAG"; @Override protected void onPause() { super.onPause(); Log.i(TAG,"onPause"); } @Override protected void onResume() { super.onResume(); Log.i(TAG,"onResume"); } @Override protected void onStart() { super.onStart(); Log.i(TAG,"onStart"); } @Override protected void onStop() { super.onStop(); Log.i(TAG,"onStop"); } @Override protected void onRestart() { super.onRestart(); Log.i(TAG,"onRestart"); } @Override protected void onDestroy() { super.onDestroy(); Log.i(TAG,"onDestroy"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i(TAG,"onCreate"); } }
1、创建class,继承自Activity的Activity
2、重写需要的回调方法
3、设置要显示的视图
4、在AndroidManifest.xml配置activity
或者
使用android studio的创建Activity的向导
入口Activity
需要在AndroidManifest.xml中进行配置
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
其他Activity
通过startActivity进行启动
Intent intent = new Intent(MainActivity.this,DetailActivity.class);
startActivity(intent);
在需要的地方调用finish()方法就行
onCreate(null);
什么时Bundle:键值对的组合
使用
1、通过putXXX方法,将数据保存到Bundle中
2、通过putExtras方法,将Bundle保存到Intent中
3、通过startActivity方法,调用intent传递数据
发送Bundle
Intent intent = new Intent(MainActivity.this,DetailActivity.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("name", "1231232");
intent.putExtras(bundle);
startActivity(intent);
接受Bundle
Intent intent1 = getIntent();
Bundle bundle1 = intent1.getExtras();
String name = bundle.getString("name");
需要使用 startActivityForResult方法
startActivityForResult(Intent intent, int requestCode);
// requestCode 请求码,标识请求的来源
在另一个Activity中返回结果
Intent intent1 = getIntent();
Bundle bundle1 = new Bundle();
bundle1.putInt("imageId",213);
setResult(0x11, intent);
finish();
接收返回的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0x07 && resultCode == 0x11){
Bundle bundle = data.getExtras();
int imageId = bundle.getInt("imageId");
}
}
可以在多个Activity中重用多个Fragment
也可以在一个Activity中使用多个Fragment

1、创建多个Fragment布局文件
2、在需要用到这些fragment布局文件的地方,用fragment标签占位
3、在java中,为每一个布局文件绑定显示和监听
4、在java中,为用到fragment的地方,进行fragment切换监听
1、
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/tongxun"
android:scaleType="fitXY"/>
</RelativeLayout>
2、
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.mingrisoft.MainActivity"> <fragment android:id="@+id/fragment" android:name="com.mingrisoft.WeChat_Fragment" android:layout_width="match_parent" android:layout_height="match_parent"/> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <ImageView android:id="@+id/image1" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:src="@drawable/bottom_1" /> <ImageView android:id="@+id/image2" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:src="@drawable/bottom_2" /> <ImageView android:id="@+id/image3" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:src="@drawable/bottom_3" /> <ImageView android:id="@+id/image4" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:src="@drawable/bottom_4" /> </LinearLayout> </RelativeLayout>
3、
package com.mingrisoft; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Administrator on 2016/2/18. */ public class WeChat_Fragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.wechat_fragment,null); return view; } }
4、
package com.mingrisoft; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView1 = (ImageView) findViewById(R.id.image1);//获取布局文件的第一个导航图片 ImageView imageView2 = (ImageView) findViewById(R.id.image2);//获取布局文件的第二个导航图片 ImageView imageView3 = (ImageView) findViewById(R.id.image3);//获取布局文件的第三个导航图片 ImageView imageView4 = (ImageView) findViewById(R.id.image4);//获取布局文件的第四个导航图片 imageView1.setOnClickListener(l);//为第一个导航图片添加单机事件 imageView2.setOnClickListener(l);//为第二个导航图片添加单机事件 imageView3.setOnClickListener(l);//为第三个导航图片添加单机事件 imageView4.setOnClickListener(l);//为第四个导航图片添加单机事件 } //创建单机事件监听器 View.OnClickListener l = new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getFragmentManager(); // 获取Fragment FragmentTransaction ft = fm.beginTransaction(); // 开启一个事务 Fragment f = null; //为Fragment初始化 switch (v.getId()) { //通过获取点击的id判断点击了哪个张图片 case R.id.image1: f = new WeChat_Fragment(); //创建第一个Fragment break; case R.id.image2: f = new Message_Fragment();//创建第二个Fragment break; case R.id.image3: f = new Find_Fragment();//创建第三个Fragment break; case R.id.image4: f = new Me_Fragment();//创建第四个Fragment break; default: break; } ft.replace(R.id.fragment, f); //替换Fragment ft.commit(); //提交事务 } }; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。