这一篇文章专门整理一下研究过的Android面试题,内容会随着学习不断的添加,假设答案有错误,希望大家能够指正
1.简述Activity的生命周期在这里保存的数据。应该在 onResume 里读出来。帮用户恢复之前的状态。
Intent intent = new Intent(this,OtherActivity.class); startActivity(intent);显式意图还有第二种形式
Intent intent = new Intent(); ComponentName component = new ComponentName(this, OtherActivity.class); intent.setComponent(component); startActivity(intent);事实上这两种形式事实上是一样的,我们看一下Intent构造函数的代码
public Intent(Context packageContext, Class<?
> cls) { mComponent = new ComponentName(packageContext, cls); }
这样我们就一目了然了。事实上我们常常使用的Intent的构造方法是另外一种方式的简化版Intent intent = new Intent(); intent.setAction("other"); startActivity(intent);隐式意图是通过setAction来进行区分究竟跳转到哪一个界面。那么我们肯定要在须要跳转的页面设置一标志。我们须要在AndroidManifest.xml中对这个进行设置
<activity android:name="com.example.lifecicledemo.OtherActivity" > <intent-filter> <action android:name="other" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
这样当我们使用setAction的时候,就能够知道我们究竟是想跳转到哪一个页面了。
3.Android中获取图片有哪几种方式
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher); img.setImageDrawable(drawable);
方式二
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); img.setImageBitmap(bitmap);
方式三
AssetManager assetManager = getResources().getAssets(); try { InputStream is = assetManager.open("ic_launcher.png"); Bitmap bitmap = BitmapFactory.decodeStream(is); img.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); }
方式四
AssetManager assetManager = getResources().getAssets(); try { InputStream is = assetManager.open("ic_launcher.png"); Drawable drawable = Drawable.createFromStream(is, null); img.setImageDrawable(drawable); } catch (IOException e) { e.printStackTrace(); }