rxjava mSubscription.unsubscribe onCreate 的工作方式是否与活动中的 onDestroy 相同?
它是否会以相同的方式工作并取消订阅以通过将其放置在 onCreate 中来防止内存泄漏,
subscription.subscribeOn().observeOn().subscribe().unsubscribe
因为不断调用 Activity 来填充下一个片段并弹回堆栈。如果 mSubscription.unsubscribe 在 Activity 的 onDestroy 中完成,它会给出一个 RunTimeException: Unable to destroy activity because of NullPointerException我在这里错过了一些生命周期问题吗?
谢谢!
private ViewPager viewPager;
private TabLayout tabLayout;
private int mPosition;
private String id;
private Subscription mSubscription;
private Context mContext;
private RealmResults<Experience> realmResults;
private String animateExtraColumnName, tag;
private Boolean automaticUpdate, animateResults;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employment);
User currentUser = AppDelegate.getCurrentUser();
assert currentUser != null;
mEmployee = getRealm().where(Employee.class).equalTo("user.id", currentUser.getId()).findFirst();
//Server returns Experience's ID
if (employeeExperience == null) {
AccessToken accessToken = currentUser.getAccessTokenAsTokenType(AppDelegate.getContext(), "Bearer");
employeeExperienceAPIOAuth2Call apiCall = new employeeExperienceAPIOAuth2Call(AppDelegate.getContext(), accessToken);
Observable<ResponseEmployeeExperience> postEmployeeExperienceObservable = apiCall.postEmployeeExperiencesRX();
mSubscription = postEmployeeExperienceObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((data) -> {
Log.d(TAG, "onCreate: Subscribe ");
employeeExperience = data.getEmployeeExperience();
Log.d(TAG, "onCreate: employeeExperience.getId()");
getRealm().beginTransaction();
getRealm().copyToRealmOrUpdate(employeeExperience);
// This adds the new employeeExperience object into the EmployeeExperiences RealmList
employee.employeeExperiences.add(employeeExperience);
getRealm().commitTransaction();
getRealm().close();
//unsubscribe here because the activity doesnt close itself to begin with?
mSubscription.unsubscribe();
Log.d(TAG, "onCreate: Unsubscribe: ");
// prepare data needed by our fragment
FragmentAddOrUpdateEmploymentHistory addEmploymentHistoryFragment = new FragmentAddOrUpdateEmploymentHistory();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.edit_frame_layout, addEmploymentHistoryFragment, "EDIT_FRAGMENT").commit();
}, (error) -> {
// TODO do whatever error handling we need to do here
Log.d(TAG, "onCreate: postEmployeeExp error");
error.printStackTrace();
});
}
} else if (employee.getEmployeeExperiences() != null) {
employeeExperience = getRealm().where(employeeExperience.class).equalTo("id", id).findFirst();
FragmentAddOrUpdateEmploymentHistory addEmploymentHistoryFragment = new FragmentAddOrUpdateEmploymentHistory();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.edit_frame_layout, addEmploymentHistoryFragment, "EDIT_FRAGMENT").commit();
}
ImageButton checkButton = (ImageButton) findViewById(R.id.edit_toolbar_check);
checkButton.setOnClickListener(this);
ImageButton crossButton = (ImageButton) findViewById(R.id.edit_toolbar_cross);
crossButton.setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
}
@Override
public void onClick(View view) {
Fragment editFragments = getSupportFragmentManager().findFragmentByTag("EDIT_FRAGMENT");
switch (view.getId()) {
case R.id.edit_toolbar_check:
if (editFragments instanceof FragmentEditDate) {
Log.d(TAG, "onClick: editdate ");
getSupportFragmentManager().popBackStack();
} else if (editFragments instanceof FragmentSalary){
Log.d(TAG, "onClick: salary ");
getSupportFragmentManager().popBackStack();
} else if (editFragments instanceof FragmentAddOrUpdateEmploymentHistory) {
Log.d(TAG, "onClick: employmenthistory ");
finish();
} else if (editFragments instanceof FragmentSingleChoice) {
Log.d(TAG, "singlechoice " );
getSupportFragmentManager().popBackStack();
}
else if (editFragments instanceof FragmentMultipleChoice) {
Log.d(TAG, "multiplechoice " );
getSupportFragmentManager().popBackStack();
}
break;
case R.id.edit_toolbar_cross:
if (editFragments instanceof FragmentAddOrUpdateEmploymentHistory) {
getRealm().beginTransaction();
employeeExperience.DELETE();
getRealm().commitTransaction();
getRealm().close();
}
finish();
} else {
finish();
}
} else {
getSupportFragmentManager().popBackStack(); }
break;
}
}