在活动A的顶部启动活动B(由应用程序类的子类的onCreate调用)(由活动类的子类的onResume调用)

时间:2017-03-06 07:46:29

标签: android android-intent android-activity

我有2个涉及此问题的活动:

  1. LoginActivity:登录屏幕(由活动类的子类调用)

  2. 更新程序活动:更新屏幕,显示新应用程序版本可用时的更新进度(由应用程序类的子类调用)

  3. 这就是我想要的:

    当应用程序首次启动时,如果应用程序的新版本可用,则 UPDATER ACTIVITY 应位于每项活动之上,包括登录活动,

    因此,首次启动应用时,第一个屏幕用户会看到

    只有在完成更新后, UPDATER ACTIVITY 才会被取消,然后登录屏幕就会显示在最佳状态。

    然而这才是实际发生的事情:

    LOGIN ACTIVITY始终位于UPDATER ACTIVITY ...

    之上

    我的猜测是,由于UPDATER ACTIVITY由APPLICATION CLASS的子类调用,而LOGINACTIVITY由ACTIVITY CLASS子类调用,

    应用类的方法(如OnResume)总是在Activity onCreate方法之前被调用,因此LOGINACTIVITY总是出现在UPDATER ACTIVITY之上,

    我只是想知道无论如何我可以让我的UPDATER ACTIVITY在任何活动之上,包括LOGIN ACTIVITY?

    //LoginActivity involved
    public abstract ClassA extends Activity {
    .....
    .....
    protected void onResume() {
        super.onResume();
        .....
        if(isAuthenticated){
            Intent intent = new Intent(this, LoginActivity.class)
                .putExtra(...)
        startActivityForResult(intent, XXX);
        }
    
    }
    

    }

    ====

    //Updater Activity involved
    public abstract class ClassB extends Application { 
    ....
    ....
    ....
    public void onCreate() {
        super.onCreate();
    
        Intent updaterIntent = new Intent(this, UpdaterActivity.class);
        updaterIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(updaterIntent);
    
    }
    

    }

1 个答案:

答案 0 :(得分:0)

我有不同的解决方案内容活动创建对话框活动。

           public class ViewDialog {

    public void showDialog(Activity activity, String msg){
        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.updatedialog);

        TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
        text.setText(msg);

        Button dialogUpdate = (Button) dialog.findViewById(R.id.update);
        Button dialogcancel = (Button) dialog.findViewById(R.id.cancel);
        Button dialogremindme = (Button) dialog.findViewById(R.id.remindeme);
        TextView textView= (TextView) dialog.findViewById(R.id.machhint1);
        TextView textView2= (TextView) dialog.findViewById(R.id.machhint2);

        textView2.setText("Your old version is : "+versionName);
        textView.setText("Current version in playstore : "+version);


        dialogcancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialogUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
                try {
                    SharedPreferences.Editor   editoradddetail1 = getSharedPreferences("later", MODE_PRIVATE).edit();

                    editoradddetail1.putString("days", "");

                    editoradddetail1.commit();
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                } catch (android.content.ActivityNotFoundException anfe) {
                    later="";
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
                }
                dialog.dismiss();
            }
        });
        dialogremindme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             SharedPreferences.Editor   editoradddetail1 = getSharedPreferences("later", MODE_PRIVATE).edit();
                Date someDate = new Date(); // Or whatever
                Date dayAfter = new Date(someDate.getTime() + TimeUnit.DAYS.toMillis( 2 ));
                DateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");

                String formattedDate=dateFormat.format(dayAfter);
                editoradddetail1.putString("days", formattedDate);

                editoradddetail1.commit();


                dialog.dismiss();
            }
        });

        dialog.show();

    }
}

//和xml是。

       <android.support.v7.widget.CardView
    android:layout_width="match_parent"


android:layout_height="250dp"
       xmlns:android="http://schemas.android.com/apk/res/android">
     <RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"

android:layout_height="250dp"
>
<RelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/main"
    android:background="#FFed6904"
    android:layout_height="120dp">



    <TextView
        android:id="@+id/machhint"

        android:text="Note:- Your current Version Did Not Match"
        android:layout_width="match_parent"
        android:textColor="@color/white"
        android:textSize="13dp"
        android:layout_height="60dp" />

    <TextView
        android:id="@+id/machhint1"
        android:text="preschool version is"
        android:layout_width="wrap_content"
        android:textSize="13dp"
        android:textColor="@color/white"
        android:layout_below="@+id/machhint"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/machhint2"
        android:text="Your  Vertion  is :"
        android:layout_width="wrap_content"
        android:textColor="@color/white"
        android:textSize="13dp"
        android:layout_below="@+id/machhint1"
        android:layout_height="30dp" />



</RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/text1"
    android:layout_below="@+id/main"
    android:layout_height="40dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TEXTO"
    android:id="@+id/text_dialog"

    android:layout_marginTop="3dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="3dp"
    android:textSize="18sp"
    android:textColor="#ff000000"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal" />
</RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_below="@+id/text1"

    android:gravity="center_vertical|center_horizontal"
    android:layout_height="55dp">

<Button
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:text="Update"
    android:id="@+id/update"
    android:gravity="center"



    android:layout_toLeftOf="@+id/remindeme"



  />
<Button
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:text="Later"
    android:id="@+id/remindeme"

    android:gravity="center"


    android:layout_centerHorizontal="true"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:text="Cancel"
    android:id="@+id/cancel"

    android:gravity="center"

    android:layout_toRightOf="@+id/remindeme"


    android:layout_centerHorizontal="true"
   />

</RelativeLayout>
    </RelativeLayout>
 </android.support.v7.widget.CardView>