Android知识点日常记录(四)

显示不重复Notification

消息通知的重点就是下面的这个方法,根据源码解释可知,我们想要通知不重复显示,就变更Id就可以。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* Post a notification to be shown in the status bar. If a notification with
* the same id has already been posted by your application and has not yet been canceled, it
* will be replaced by the updated information.
*
* @param id An identifier for this notification unique within your
* application.
* @param notification A {@link Notification} object describing what to show the user. Must not
* be null.
*/
public void notify(int id, Notification notification)
{
notify(null, id, notification);
}

解决方案:

将时间戳作为Id,传入方法中。

1
2
3
int notifyId = (int) System.currentTimeMillis();
// 发出通知
notificationManager.notify(notifyId, builder.build());

神奇的super

super.onBackPressed()

项目中需要在Back键返回的时候,传值给上一个界面,重写了onBackPressed()方法,正常传值。代码运行与想象不一致,然后就在想是什么问题,试着把super.onBackPressed();父类调用放在最后,编译运行代码,符合预期。

直接调用父类的super.onBackPressed();不会走子类的传值,页面已经finish()掉,所以需要此类场景的传值,需要将父类调用放在最后。

1
2
3
4
5
6
7
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("picture", selImageList);
setResult(RESULT_OK, intent);
super.onBackPressed();
}

super.onBindViewHolder(viewHolder, position)

RecyclerViewAdapter没有每个item点击事件,需要自己去写接口监听,写了一个BaseAdapter,点击事件监听Ok,处理加载更多,不小心删掉了super.onBindViewHolder(viewHolder, position),点击事件失效。添加父类调用,点击事件Ok。

经过这两个日常问题的处理,更加深刻的理解了super的重要性。

Parcel: unable to marshal value

在两个Activity直接传递List<XXInfo>时,出现Parcel: unable to marshal value异常。
必须强制类型转换为ArrayList<XXInfo>

OneActivity页面(OneActivity页面向NextActivity页面传递一个List<XXInfo>):

1
2
3
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("list", list);
startActivity(intent);

解决方案:

XXInfoimplements Serializable或者继承Parcelable,
list必须是ArrayList(若是List会提示错误)。

1
2
MainActivity中,intent.putExtra("list", Arraylist实例)。
NextActivity中,List<xxInfo> infoList = (ArrayList) getIntent().getSerializableExtra("list")