我正在使用MVC制作Android应用程序。我使用Activity作为Controller,使用不同的类作为View。
View正在等待两个异步任务,一个Google Map和一个从数据库中获取数据的任务。 View需要数据库中的数据才能在地图上放置标记。如果地图首先加载,我们就无法放置标记。如果数据库任务完成,那么我们需要等待加载地图。
如何检查相关任务是否已完成? 我是否应该有一个标志来说明数据库任务是否完成,然后加载地图时检查这是否继续,反之亦然。 或者有更好的方法来做这一切。
这是View的简化版本:
public class SellerAddView
implements OnChangeListener<Model>, OnMapReadyCallback{
...
public SellerAddView(View view, Model model, Activity activity){
model.addListener(this);
mapFragment = ((MapFragment)activity.getFragmentManager()
.findFragmentById(R.id.add_map));
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng coords = model.getLatLng();
map.addMarker(new MarkerOptions().position(coords));
}
@Override
public void onChange(SellerAddModel model) {
updateView();
}
}
这是简化的控制器:
public class Controller extends Activity{
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View screenView = View.inflate(this, activity_seller_add, null);
model = new Model();
view = new view(screenView, model, this);
populateModel();
setContentView(screenView);
}
public void populateModel(){
handler.post(new Runnable() {
@Override
public void run() {
synchronized (model) {
Model newModel = new ModelDao().getId(id);
model.consume(model);
}
}
});
}
}
答案 0 :(得分:3)
因此,您可以使用Splash screen
等待加载所有数据,这样会更好。
示例代码如下:
public class SplashScreen extends Activity {
private static int SPLASH_DELEY = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_DELEY);
}
}
对于activity_splash_screen.xml
:
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.bjiang.map_ex.SplashScreen">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageLogo"
android:layout_centerInParent="true"
android:src="@drawable/splash_file"/>
</RelativeLayout>