MVP(Model-View-Presenter)是一种设计模式,用于将应用程序的业务逻辑、用户界面和数据分开。在Android开发中,MVP模式可以帮助开发者更好地组织代码,提高代码的可维护性和可测试性。
在Android开发中,Activity和Fragment是两种常见的UI组件。Activity是一个独立的屏幕,而Fragment是Activity的一部分,可以嵌入到Activity中。当从Activity迁移到Fragment时,数据传递的方式需要进行相应的调整。
MVP模式适用于需要高度解耦和可测试性的应用场景,例如:
假设我们有一个简单的应用,显示用户的个人信息。我们从Activity迁移到Fragment,并使用MVP模式进行数据传递。
// Model
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
// View
public interface UserView {
void showUserInfo(User user);
}
// Presenter
public class UserPresenter {
private UserView view;
private User user;
public UserPresenter(UserView view) {
this.view = view;
this.user = new User("John Doe", 30);
}
public void loadUserInfo() {
view.showUserInfo(user);
}
}
// Activity
public class MainActivity extends AppCompatActivity implements UserView {
private UserPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
presenter = new UserPresenter(this);
presenter.loadUserInfo();
}
@Override
public void showUserInfo(User user) {
TextView nameTextView = findViewById(R.id.nameTextView);
TextView ageTextView = findViewById(R.id.ageTextView);
nameTextView.setText(user.getName());
ageTextView.setText(String.valueOf(user.getAge()));
}
}
// Fragment
public class UserFragment extends Fragment implements UserView {
private UserPresenter presenter;
private TextView nameTextView;
private TextView ageTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_user, container, false);
nameTextView = view.findViewById(R.id.nameTextView);
ageTextView = view.findViewById(R.id.ageTextView);
presenter = new UserPresenter(this);
presenter.loadUserInfo();
return view;
}
@Override
public void showUserInfo(User user) {
nameTextView.setText(user.getName());
ageTextView.setText(String.valueOf(user.getAge()));
}
}
原因:Fragment的生命周期与Activity不同,可能会导致Presenter在Fragment销毁时仍然持有引用,从而引发内存泄漏。
解决方法:
public class UserPresenter {
private WeakReference<UserView> viewRef;
private User user;
public UserPresenter(UserView view) {
this.viewRef = new WeakReference<>(view);
this.user = new User("John Doe", 30);
}
public void loadUserInfo() {
UserView view = viewRef.get();
if (view != null) {
view.showUserInfo(user);
}
}
}
onDestroyView
方法中释放Presenter中的引用。@Override
public void onDestroyView() {
super.onDestroyView();
presenter.detachView();
}
// 修改Presenter
public class UserPresenter {
private WeakReference<UserView> viewRef;
private User user;
public UserPresenter(UserView view) {
this.viewRef = new WeakReference<>(view);
this.user = new User("John Doe", 30);
}
public void loadUserInfo() {
UserView view = viewRef.get();
if (view != null) {
view.showUserInfo(user);
}
}
public void detachView() {
viewRef.clear();
}
}
通过以上示例和解释,你应该能够理解从Activity到Fragment的MVP数据传递方式及其相关概念和解决方法。
领取专属 10元无门槛券
手把手带您无忧上云