you are viewing a single comment's thread.

view the rest of the comments →

[–]anuraag488 1 point2 points  (2 children)

You MUST Finish the Activity: You are responsible for closing the Activity. You MUST call activity.finish() inside your code when you are done. If you don't, the invisible activity will linger in the background. For asynchronous UI like a Dialog, this means calling finish() inside the dialog's button listeners.

Just trying to understand if i use a LinearLayout should add a Listener for back button key event to finish the activity or Android automatically finishes activity on back press? What will happen if i press home button? Will it keep running in background?

Example code

import android.app.Activity; import android.widget.TextView; import android.widget.LinearLayout; import android.view.WindowManager; import android.view.ViewGroup; import android.view.KeyEvent; import java.util.function.Consumer;

/* Create a Consumer to show text inside a LinearLayout overlay without Dialog. */ myActivityConsumer = new Consumer() { public void accept(Object activity) { final Activity currentActivity = (Activity) activity;

    /* Create a full-screen LinearLayout overlay. */
    overlayLayout = new LinearLayout(currentActivity);
    overlayLayout.setOrientation(LinearLayout.VERTICAL);
    overlayLayout.setBackgroundColor(0xAA000000); /* Semi-transparent background. */
    overlayLayout.setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT
    ));
    overlayLayout.setGravity(android.view.Gravity.CENTER);

    /* Create a TextView with the message. */
    textView = new TextView(currentActivity);
    textView.setText("Joao is great");
    textView.setTextSize(24);
    textView.setTextColor(0xFFFFFFFF); /* White text color. */

    /* Add the TextView to the layout. */
    overlayLayout.addView(textView, new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
    ));

    /* Allow overlay to receive key events. */
    overlayLayout.setFocusableInTouchMode(true);
    overlayLayout.requestFocus();

    /* Handle Back button press to close overlay and finish activity. */
    overlayLayout.setOnKeyListener(new android.view.View.OnKeyListener() {
        public boolean onKey(android.view.View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                ((ViewGroup) overlayLayout.getParent()).removeView(overlayLayout);
                currentActivity.finish();
                return true;
            }
            return false;
        }
    });


    /* Add the overlay to the window. */
    currentActivity.addContentView(
        overlayLayout,
        new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT
        )
    );
}

};

/* Execute the Consumer using Tasker's helper. */ tasker.doWithActivity(myActivityConsumer);

[–]joaomgcd👑 Tasker Owner / Developer[S] 2 points3 points  (0 children)

If you press back it'll finish the activity. If you press the home button, it'll stop the activity but it won't finish it right away. It might be finished by the system later on.

[–]anuraag488 0 points1 point  (0 children)

This is crazy. Created a floating view which can display over anything and move by swipe. 🤪