启动谷歌地图意图后返回活动

Posted

技术标签:

【中文标题】启动谷歌地图意图后返回活动【英文标题】:Back to activity after launching google map intent 【发布时间】:2018-02-09 12:17:31 【问题描述】:

我有一个带有片段的 MainActivity,当用户点击片段时,用户将导航到另一个片段,在最后一个片段上有一个启动谷歌地图导航的按钮,这是代码:

@Override
public void onClick(View v) 
    String url = "https://www.google.com/maps/dir/?api=1&destination=-17.174498,12.634206";
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(intent);
    getActivity().overridePendingTransition(R.anim.slide_up_in, R.anim.slide_up_out);

问题是,地图启动后,我希望用户在按下返回时返回我的应用程序,而不是启动器。

有没有办法做到这一点。

这是我的完整代码:

MainActivity

public class MainActivity extends AppCompatActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 

        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) 
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new SplashFragment())
                    .commit();
        
    

SplashFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_splash, container, false);
    view.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 

            Transition transition = new Fade(Visibility.MODE_IN);
            transition.setDuration(400);

            Fragment fragment = MapsFragment.newInstance();

            // set transition animation
            fragment.setEnterTransition(transition);
            fragment.setAllowEnterTransitionOverlap(true);
            fragment.setAllowReturnTransitionOverlap(true);

            // add second fragment by replacing first
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .addToBackStack(null)
                    .commit();
        
    );

    return view;

日志

08-31 21:45:41.072 1536-1536/? E/Zygote: v2
08-31 21:45:41.072 1536-1536/? I/libpersona: KNOX_SDCARD checking this for 10319
08-31 21:45:41.072 1536-1536/? I/libpersona: KNOX_SDCARD not a persona
08-31 21:45:41.072 1536-1536/? W/SELinux: Function: selinux_compare_spd_ram, index[1], priority [2], priority version is VE=SEPF_SECMOBILE_6.0.1_0034
08-31 21:45:41.082 1536-1536/? E/Zygote: accessInfo : 0
08-31 21:45:41.082 1536-1536/? W/SELinux: SELinux: seapp_context_lookup: seinfo=default, level=s0:c512,c768, pkgname=com.nyongrand.ibnuratihinvitation 
08-31 21:45:41.082 1536-1536/? I/art: Late-enabling -Xcheck:jni
08-31 21:45:41.152 1536-1536/com.nyongrand.ibnuratihinvitation W/ResourcesManager: Resource getTopLevelResources for package com.nyongrand.ibnuratihinvitationoverlayDirs =Null
08-31 21:45:41.342 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=speed --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.nyongrand.ibnuratihinvitation-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.nyongrand.ibnuratihinvitation-2@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
08-31 21:45:41.592 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=speed --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.nyongrand.ibnuratihinvitation-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.nyongrand.ibnuratihinvitation-2@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
08-31 21:45:41.652 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=speed --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.nyongrand.ibnuratihinvitation-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.nyongrand.ibnuratihinvitation-2@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
08-31 21:45:41.712 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=speed --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.nyongrand.ibnuratihinvitation-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.nyongrand.ibnuratihinvitation-2@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
08-31 21:45:41.782 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=speed --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.nyongrand.ibnuratihinvitation-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.nyongrand.ibnuratihinvitation-2@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
08-31 21:45:41.842 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=speed --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.nyongrand.ibnuratihinvitation-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.nyongrand.ibnuratihinvitation-2@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
08-31 21:45:41.902 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=speed --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.nyongrand.ibnuratihinvitation-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.nyongrand.ibnuratihinvitation-2@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
08-31 21:45:41.972 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=speed --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.nyongrand.ibnuratihinvitation-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.nyongrand.ibnuratihinvitation-2@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
08-31 21:45:42.032 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=speed --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.nyongrand.ibnuratihinvitation-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.nyongrand.ibnuratihinvitation-2@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
08-31 21:45:42.092 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=speed --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.nyongrand.ibnuratihinvitation-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.nyongrand.ibnuratihinvitation-2@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
08-31 21:45:42.162 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=speed --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.nyongrand.ibnuratihinvitation-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.nyongrand.ibnuratihinvitation-2@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
08-31 21:45:42.162 1536-1536/com.nyongrand.ibnuratihinvitation W/System: ClassLoader referenced unknown path: /data/app/com.nyongrand.ibnuratihinvitation-2/lib/arm64
08-31 21:45:42.172 1536-1536/com.nyongrand.ibnuratihinvitation I/InstantRun: starting instant run server: is main process
08-31 21:45:42.322 1536-1536/com.nyongrand.ibnuratihinvitation W/art: Before android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-31 21:45:42.532 1536-1705/com.nyongrand.ibnuratihinvitation I/zzbx: Making Creator dynamically
08-31 21:45:42.542 1536-1705/com.nyongrand.ibnuratihinvitation W/ResourcesManager: Resource getTopLevelResources for package com.google.android.gmsoverlayDirs =Null
08-31 21:45:42.602 1536-1708/com.nyongrand.ibnuratihinvitation I/OpenGLRenderer: Initialized EGL, version 1.4
08-31 21:45:42.652 1536-1542/com.nyongrand.ibnuratihinvitation W/art: Suspending all threads took: 6.789ms
08-31 21:45:42.662 1536-1705/com.nyongrand.ibnuratihinvitation W/ResourcesManager: Resource getTopLevelResources for package com.google.android.gmsoverlayDirs =Null
08-31 21:45:42.672 1536-1536/com.nyongrand.ibnuratihinvitation I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@9e7090d time:16980299
08-31 21:45:42.712 1536-1705/com.nyongrand.ibnuratihinvitation I/Google Maps Android API: Google Play services client version: 11020000
08-31 21:45:42.732 1536-1705/com.nyongrand.ibnuratihinvitation I/Google Maps Android API: Google Play services package version: 11509440
08-31 21:45:43.322 1536-1705/com.nyongrand.ibnuratihinvitation E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@de4bdd2
08-31 21:45:43.322 1536-1705/com.nyongrand.ibnuratihinvitation E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@ea6fea3
08-31 21:45:43.322 1536-1705/com.nyongrand.ibnuratihinvitation E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@ea6fea3
08-31 21:45:43.382 1536-1705/com.nyongrand.ibnuratihinvitation W/System.err: mkdir failed: EEXIST (File exists) : /storage/emulated/0/Android/data/com.nyongrand.ibnuratihinvitation/cache/debug
08-31 21:45:43.382 1536-1705/com.nyongrand.ibnuratihinvitation W/System.err: mkdir failed: EEXIST (File exists) : /storage/emulated/0/Android/data/com.nyongrand.ibnuratihinvitation/cache
08-31 21:45:43.432 1536-1793/com.nyongrand.ibnuratihinvitation I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 21:45:43.432 1536-1793/com.nyongrand.ibnuratihinvitation I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 21:45:43.442 1536-1793/com.nyongrand.ibnuratihinvitation I/qtaguid: Tagging socket 85 with tag 3000110100000000805310721,0 uid -1, pid: 1536, getuid(): 10319
08-31 21:45:43.842 1536-1793/com.nyongrand.ibnuratihinvitation I/qtaguid: Tagging socket 79 with tag 3000110100000000805310721,0 uid -1, pid: 1536, getuid(): 10319
08-31 21:45:44.002 1536-1793/com.nyongrand.ibnuratihinvitation I/qtaguid: Untagging socket 85
08-31 21:45:57.962 1536-1536/com.nyongrand.ibnuratihinvitation I/Google Maps Android API: Google Play services package version: 11509440
08-31 21:45:58.102 1536-1795/com.nyongrand.ibnuratihinvitation I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 21:45:58.102 1536-1795/com.nyongrand.ibnuratihinvitation I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 21:45:58.102 1536-1795/com.nyongrand.ibnuratihinvitation I/qtaguid: Tagging socket 85 with tag 3000110100000000805310721,0 uid -1, pid: 1536, getuid(): 10319
08-31 21:45:58.192 1536-1796/com.nyongrand.ibnuratihinvitation I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 21:45:58.192 1536-1796/com.nyongrand.ibnuratihinvitation I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 21:45:58.192 1536-1796/com.nyongrand.ibnuratihinvitation I/qtaguid: Tagging socket 58 with tag 3000110100000000805310721,0 uid -1, pid: 1536, getuid(): 10319
08-31 21:45:58.282 1536-1795/com.nyongrand.ibnuratihinvitation I/qtaguid: Untagging socket 85
08-31 21:45:58.452 1536-1796/com.nyongrand.ibnuratihinvitation I/qtaguid: Tagging socket 61 with tag 3000110100000000805310721,0 uid -1, pid: 1536, getuid(): 10319
08-31 21:45:58.682 1536-1796/com.nyongrand.ibnuratihinvitation I/qtaguid: Untagging socket 58
08-31 21:45:59.352 1536-1797/com.nyongrand.ibnuratihinvitation I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 21:45:59.352 1536-1797/com.nyongrand.ibnuratihinvitation I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 21:45:59.362 1536-1797/com.nyongrand.ibnuratihinvitation I/qtaguid: Tagging socket 93 with tag 3000110100000000805310721,0 uid -1, pid: 1536, getuid(): 10319
08-31 21:46:00.182 1536-3184/com.nyongrand.ibnuratihinvitation W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
08-31 21:46:00.202 1536-3184/com.nyongrand.ibnuratihinvitation I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
08-31 21:46:00.202 1536-3184/com.nyongrand.ibnuratihinvitation I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 4
08-31 21:46:00.222 1536-3184/com.nyongrand.ibnuratihinvitation W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000014/n/arm64-v8a
08-31 21:46:00.232 1536-3184/com.nyongrand.ibnuratihinvitation W/ResourcesManager: Resource getTopLevelResources for package com.google.android.gmsoverlayDirs =Null
08-31 21:46:01.522 1536-1797/com.nyongrand.ibnuratihinvitation I/qtaguid: Tagging socket 103 with tag 3000110100000000805310721,0 uid -1, pid: 1536, getuid(): 10319
08-31 21:46:01.922 1536-1797/com.nyongrand.ibnuratihinvitation I/qtaguid: Untagging socket 93

【问题讨论】:

在开始活动后为getActivity().onBackpressed()编写代码; 您可以在您的应用程序developers.google.com/maps/documentation/android-api/start 中使用谷歌地图。万一找不到解决方案。 @HimeshGoswami 没有 @SahdeepSingh 实际上我已经这样做了,我想要谷歌地图轮流导航功能,所以我启动了谷歌地图。 @nyongrand 这可能行得通***.com/questions/24531391/… 但对于你的问题我不能说什么 【参考方案1】:

您必须向意图添加标志,如下所示

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.slide_up_in, R.anim.slide_up_out);

请尝试告诉我。

【讨论】:

不起作用,当按回谷歌地图时,android 只是回到启动器。 尝试不覆盖事务 getActivity().overridePendingTransition(R.anim.slide_up_in, R.anim.slide_up_out); 这对我来说很好用,即使没有标志它也可以正常工作.. 可能是我们看错了地方......您是否收到任何警告或异常。您是否在主要活动中添加了任何标志,例如在意图或清单中从最近的内容中排除? 我第一次尝试可以,但是当我再次尝试时,没有更改任何代码,它不起作用,即使在清除所有最近的应用程序后,它仍然不起作用, 请检查是否为主要活动设置了任何标志,从一个片段导航到另一个片段的代码是什么?你在哪里打电话完成?打开谷歌地图后,您的应用是否在最近显示中可见?你的 onDestroy 被调用了吗?你得到什么日志?我问你这么多问题,因为你提供的代码 sn-p 不足以回答你的问题。抱歉问了这么多问题..

以上是关于启动谷歌地图意图后返回活动的主要内容,如果未能解决你的问题,请参考以下文章

试图从谷歌地图获取纬度是通过 Android 中的意图返回 null

回到地图意图espresso - UI测试的活动

我想通过我的应用程序通过意图启动谷歌地图开始驾驶模式[重复]

从 Maps Intent espresso 回到活动 - UI 测试

从谷歌地图应用程序按下后恢复我的应用程序

通过意图将地址传递给 Google 地图活动: