简体   繁体   English

一次打开主要活动

[英]Open main-activity one time

For my news-reader app, I made a widget.对于我的新闻阅读器应用程序,我制作了一个小部件。 Using the widget it is possible to start the news-app.使用小部件可以启动新闻应用程序。 But if the user goes back to the home screen, with the back button, there still is an instance of the application.但是,如果用户使用返回按钮返回主屏幕,仍然有一个应用程序实例。 So if the user goes the applications-list (all the app's) and start the news-app again.因此,如果用户进入应用程序列表(所有应用程序)并再次启动新闻应用程序。 There are 2 instances of the news app:S For closing the app, the users needs to push 2 times on the back button (because you see 2 times the same app).新闻应用有 2 个实例:S 要关闭应用,用户需要按 2 次返回按钮(因为您会看到 2 次同一个应用)。 Can I do anything about it?我能做些什么吗?

Here my code:这是我的代码:

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
        int appWidgetId) {


    Intent configIntent = new Intent(context, Main.class);
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

    remoteViews.setOnClickPendingIntent(R.id.headlinesBox, configPendingIntent);

    // Tell the widget manager
    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

} }

You can set true on android:clearTaskOnLaunch activity attribute in the Manifest file.您可以在 Manifest 文件中的android:clearTaskOnLaunch活动属性上设置为 true。

It should always close all the extra Activities and start the App from the first Activity.它应该始终关闭所有额外的活动并从第一个活动启动应用程序。

It seems that you have to add to the manifest:看来您必须添加到清单中:

    <activity android:name=".Main"
              android:launchMode="singleInstance">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
        </intent-filter>
    </activity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Coudnt在main-activity中调用数组 - Coudnt call the array in main-activity 如何根据android应用程序中的日期更改主活动中的图像 - How to change image in main-activity based on date in android application 如何实现只有一次登录活动在启动画面下次打开后,它将在android中打开直接主要活动 - How to Implement Only one time login activity open after splash screen next time it will open direct main activity in android 如何减少android中main-activity的警报对话代码? - How do I reduce alert-dialogue code from main-activity in android? Android - Java - 从另一个实例类访问Main-Activity中的私有变量 - Android - Java - Access private variable in Main-Activity from another instanced class 从主要活动中打开MultipleFragment - Open MultipleFragment from main activity 如何将数据从一次打开的活动传输到下一次 - How to transfer data from one time open activity to next 如何使用意图在主要活动布局顶部打开活动? - How to open an activity on top of the main activity layout using intent? 将导航抽屉从第二个活动添加到activity_main。 在主活动中使用.opendrawer打开第二个 - Adding navigation drawer from second activity to activity_main. Using .opendrawer in main activity to open second 如何从 Kotlin 的主要活动中打开多个活动? - How to open more than activity from Main Activity in Kotlin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM