[英]start Activity B(called by onCreate of an Application Class's sub Class) on Top of Activity A(called by onResume of an Activity Class's sub Class)
我有2個活動涉及此問題:
LoginActivity:屏幕中的登錄名(由“活動類”的子類調用)
更新程序活動:一個更新屏幕,在有新的應用程序版本可用時顯示更新進度(由應用程序類的子類調用)
這就是我要的:
當應用程序首次啟動時 ,如果應用程序有新版本可用,則更新程序活動應位於每個活動(包括登錄活動,
因此它將是用戶首次啟動該應用時看到的第一個屏幕 。
僅在完成更新進度后,“更新器活動”將被取消,然后“登錄”屏幕位於頂部。
但這是實際發生的情況:
LOGIN ACTIVITY始終位於UPDATER ACTIVITY ...
我的猜測是,由於UPDATER ACTIVITY由APPLICATION CLASS的子類調用,而LOGINACTIVITY由ACTIVITY CLASS子類調用,
Application類的方法(例如OnResume)總是在Activity onCreate方法之前被調用,因此LOGINACTIVITY總是出現在UPDATER 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);
}
}
我有不同的解決方案意圖活動創建對話框活動。
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.