从2种不同的活动中返回。如何知道我来自哪里

时间:2018-11-22 22:49:37

标签: android android-intent android-activity

所以我有一个返回的活动将我带回Int的主要活动。棘手的是,有两种方法可以找回。要么返回一个位置ID,要么返回一个路线ID。如果返回的是位置ID,则会在地图上创建一个标记。但是,如果返回路线ID,则会在地图上画一条线。我的问题是我没有任何办法知道我是否返回带有位置ID或路线ID的主要活动。

if(requestCode == REQUEST_CODE_BOOKMARKS_ACTIVITY)
    {
        if(data != null)
        {
            if(// Check if it's a location id)
            {
                int locationId = data.getIntExtra(BookmarksActivity.LOCATION_ID, 1);
                Log.i("BBB",    locationId + "");
                myDataSource.open();
                com.zayed.zma_at3.model.Location savedLocation = myDataSource.getLocationByID(locationId);
                createMarkerOnLocation(savedLocation.getTitle(),savedLocation.getLat(),savedLocation.getLng());
            }
            else
            {
                int routeId = data.getIntExtra(BookmarksActivity.ROUTE_ID, 1);
                myDataSource.open();
                Route route = myDataSource.getRouteById(routeId);
                route.setLocationPoints(myDataSource.getPointsByRouteId(routeId));

                // Draw line on map
                for(com.zayed.zma_at3.model.Location p : route.getLocationPoints())
                    {
                        Log.i("BBB",  route.getId() +" "+ p.getId() + " " + p.getLat()
                                + " " + p.getLng() + "");
                    }
            }
        }

这是我从那里回来的地方 位置:

Intent intent = new Intent();
                intent.putExtra(BookmarksActivity.LOCATION_ID,locationID);
                intent.putExtra(BookmarksActivity.BOOKMARK_TYPE,1);
                ((Activity) context).setResult(Activity.RESULT_OK, intent);
                ((Activity) context).finish();

路线:

Intent intent = new Intent();
                intent.putExtra(BookmarksActivity.ROUTE_ID,routeID);
                intent.putExtra(BookmarksActivity.BOOKMARK_TYPE,2);
                ((Activity) context).setResult(Activity.RESULT_OK, intent);
                ((Activity) context).finish();

1 个答案:

答案 0 :(得分:0)

当您将位置ID放在意向附加项目上时,还要将路线ID添加为-1(或在您的情况下仍然无效的路径),反之亦然。检查活动结果时,请执行以下操作:

   //if the location id is not equal to -1
if (data.getIntExtra(BookmarksActivity.LOCATION_ID, -1) != -1) {
   // it means that there is a location id so do what you want with that
} 

与您的路线ID相同

编辑以包含代码:

Intent intent = new Intent(); 
intent.putExtra(BookmarksActivity.LOCATION_ID,locationID);
intent.putExtra(BookmarksActivity.ROUTE_ID, -1)
intent.putExtra(BookmarksActivity.BOOKMARK_TYPE,1);
((Activity) context).setResult(Activity.RESULT_OK, intent);
((Activity) context).finish();

还:

Intent intent = new Intent();
intent.putExtra(BookmarksActivity.ROUTE_ID,routeID);
intent.putExtra(BookmarksActivity.LOCATION_ID,-1);
intent.putExtra(BookmarksActivity.BOOKMARK_TYPE,2);
((Activity) context).setResult(Activity.RESULT_OK, intent);
((Activity) context).finish();

然后显示结果:

if(requestCode == REQUEST_CODE_BOOKMARKS_ACTIVITY)
{
    if(data != null)
    {
        int temp = data.getIntExtra(BookmarksActivity.LOCATION_ID, -1);
    int temp2 = data.getIntExtra(BookmarksActivity.ROUTE_ID, -1);

        if(temp != -1)
        {
            int locationId = data.getIntExtra(BookmarksActivity.LOCATION_ID, 1);
            Log.i("BBB",    locationId + "");
            myDataSource.open();
            com.zayed.zma_at3.model.Location savedLocation = myDataSource.getLocationByID(locationId);
            createMarkerOnLocation(savedLocation.getTitle(),savedLocation.getLat(),savedLocation.getLng());
        }
        else if(temp2 != -1)
        {
            int routeId = data.getIntExtra(BookmarksActivity.ROUTE_ID, 1);
            myDataSource.open();
            Route route = myDataSource.getRouteById(routeId);
            route.setLocationPoints(myDataSource.getPointsByRouteId(routeId));

            // Draw line on map
            for(com.zayed.zma_at3.model.Location p : route.getLocationPoints())
                {
                    Log.i("BBB",  route.getId() +" "+ p.getId() + " " + p.getLat()
                            + " " + p.getLng() + "");
                }
        }
    }