0

I want to show Dialog when I'm at home screen after 10s,

and I modify my Theme to Theme.Dialog,

And now,I successfully pop it up when I pressed Back Key to home screen.

But my question is that after I pressed Home Key and go to home screen,

dialog won't pop up after 10s ,

However,when I open my application,

I found that it has popped up there.

So,can anybody told me how to solve this problem?

this is my code(I use two activities):

First Activity:

public class MainActivity extends Activity {    
private Button bt_dialog;
AlertDialog.Builder builder;
AlertDialog mDialog;
Intent i = new Intent();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bt_dialog = (Button) findViewById(R.id.button1);
    i.setClass(MainActivity.this, DialogActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);      
            //click button and show the dialog after 10s
    bt_dialog.setOnClickListener(new Button.OnClickListener() {

    @Override
    public void onClick(final View view) {
        CountDownTimer dlgCountDown;
        dlgCountDown = new CountDownTimer(10000, 1000){
        public void onTick(long millisUntilFinished) { 
        } 
        public void onFinish(){
            startActivity(i); 
        } 
        }.start(); 
        }
    });
}

Second Activity:

public class DialogActivity extends Activity{   
AlertDialog.Builder builder;
AlertDialog mDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_dialog);   
    String dialogText = "dialog text";
        TextView txt = (TextView) findViewById(R.id.textView1);
        txt.setText(dialogText);
        Button dismissbutton = (Button) findViewById(R.id.button1);
        dismissbutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            DialogActivity.this.finish();
      }
    });
  }

And this is my Manifest code:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.testdialog.MainActivity"
        android:theme="@android:style/Theme"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity 
        android:name="com.example.testdialog.DialogActivity"
        android:theme="@android:style/Theme.Dialog">
    </activity>

MDMalik
  • 3,951
  • 2
  • 25
  • 39
sheu46
  • 27
  • 1
  • 6

2 Answers2

0

Your code is working properly. If I'm not mistaken your issue is that the Event On the Button was not working. When coming back from Activity 2 to Activity1

I just added Log at different places in your code so as you could know what is happening and on which location.

MainActivity

public class MainActivity extends Activity {
private Button bt_dialog;
AlertDialog.Builder builder;
AlertDialog mDialog;
Intent i = new Intent();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bt_dialog = (Button) findViewById(R.id.button1);
    i.setClass(Experiment.this, DialogActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    // click button and show the dialog after 10s
    Log.i("MainActivity ", "Create");
    bt_dialog.setOnClickListener(new Button.OnClickListener() {

        public void onClick(final View view) {
            Log.i("MainActivity ", "onClick--Start");
            CountDownTimer dlgCountDown;
            dlgCountDown = new CountDownTimer(10000, 1000) {
                public void onTick(long millisUntilFinished) {
                }

                public void onFinish() {
                    Log.i("MainActivity ment", "onClick-- Finish");
                    startActivity(i);
                }
            }.start();
        }

    });
   }
}

Second Activity:

public class DialogActivity extends Activity {
AlertDialog.Builder builder;
AlertDialog mDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_dialog);
    String dialogText = "dialog text";
    TextView txt = (TextView) findViewById(R.id.textView1);
    txt.setText(dialogText);
    Log.i("Dialog", "onCreate");
    Button dismissbutton = (Button) findViewById(R.id.button1);
    dismissbutton.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {
            Log.i("Dialog", "onClick");
            DialogActivity.this.finish();
        }
    });
   }
}

Log Cat Output

05-25 10:33:14.007: I/dalvikvm-heap(19907): Grow heap (frag case) to 16.102MB for 3821200-byte allocation
05-25 10:33:14.117: I/dalvikvm-heap(19907): Grow heap (frag case) to 22.580MB for 6793232-byte allocation
05-25 10:33:14.267: I/MainActivity(19907): Create
05-25 10:33:19.267: I/MainActivity(19907): onClick--Start
05-25 10:33:29.267: I/MainActivity(19907): onClick-- Finish
05-25 10:33:29.337: I/Dialog(19907): onCreate
05-25 10:33:34.007: I/Dialog(19907): onClick
05-25 10:33:41.288: I/MainActivity(19907): onClick--Start
05-25 10:33:51.297: I/MainActivity(19907): onClick-- Finish
05-25 10:33:51.357: I/Dialog(19907): onCreate

So it seems like your Button in MainActivity is working fine

MDMalik
  • 3,951
  • 2
  • 25
  • 39
  • thank for your answer.But my problem is that I can successfully pop it up when I pressed Back Key to home screen.But the dialog will not show after I pressed Home Key and go to home screen. However,when I open my application, I found that it has popped up there. – sheu46 May 25 '13 at 07:51
  • @sheu46 ok plz explain a) Main --> Dialog --( dialog button) --> Main (and again and again) Working Great b) Main --> Dialog --(back button) --> Main (and again and again ) Working Great c) main --> Dialog -- (home button) --> Dialog --> (button or back) ---> Main (Working Great... So where is the exact issue A/B/C – MDMalik May 25 '13 at 08:01
  • I pressed Home Key and go to home screen. But the dialog will not show after 10s. – sheu46 May 25 '13 at 08:59
0

I resolved it just by adding this in the manifest file:

android:theme="@style/Theme.AppCompat.Dialog.Alert".

Add that in the application tag.

Pang
  • 9,564
  • 146
  • 81
  • 122
Arjun G
  • 606
  • 1
  • 7
  • 20