0

I have a FragmentA..that is a list fragment,it loads a list of contacts and displays them similar to what a phonebook would do.Now if I click on a contact say "John Doe" the app will launch another app (Sim Tool Kit in my case) and also display the toast with his name and number for the user to use while using the STK. Once the user completes using the STK or navigates back to the App then the toast should disappear. But in my case the Toast still persists and in some cases even when I exit my app the toast will appear infinitely. How do I correct this? Below are snippets of my Code

list.setOnItemClickListener(new OnItemClickListener() {
    @SuppressLint("RtlHardcoded")
    @SuppressWarnings("deprecation")
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
        @SuppressWarnings("unchecked")
        HashMap<String, String> h = (HashMap<String, String>) list.getAdapter().getItem(position);
        String Name="",Number="";
        Name = h.get(KEY_NAME);
        Number = h.get(KEY_PHONENUMBER);

        toast = Toast.makeText(context, "", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP|Gravity.RIGHT, 0, 0);
        toast.setView(layout);  
        toast.cancel();

        userNameText.setText(Name);
        userNumberText.setText(Number);
        PersistToast();
        getActivity().startService(ServiceIntent);

        try {
            if (!ReceiverON) {
                getActivity().registerReceiver(objReceiver, filter);
                ReceiverON=true;
            }
        } catch (IllegalArgumentException illegalargumentexception) {
            illegalargumentexception.printStackTrace();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

The persist Toast method

private void PersistToast() {
    long delay =  1000;
    long period = 1000; 
    mDoTask = new TimerTask() {
        @Override
        public void run() {                    
            toast.show();
        }
    };

    mT.scheduleAtFixedRate(mDoTask, delay, period);
}

The Receiver

private class Receiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {
        if (!(currentTask().equals("com.android.stk"))){
            try {
                if(toast != null) {
                    toast.cancel();
                    mDoTask.cancel();
                }
            } catch(Exception e) {
            }
        }
    }
}

public String currentTask() {
    return ((android.app.ActivityManager.RunningTaskInfo)((ActivityManager)context.getSystemService("activity")).getRunningTasks(1).get(0)).topActivity.getPackageName().trim();
}

The service pings the receiver periodically so that the receiver in turn checks to if the current Activity in front is the STK,if it is not then it cancels the toast and the timer task

Oleksii K.
  • 5,359
  • 6
  • 44
  • 72
James Wahome
  • 588
  • 11
  • 31
  • I don't think that you should alter the way that the toast is meant to work; which is really just a quick response to the user. You should probably design it such that there is a window of information in the screen where they are and need it. – Jay Snayder Oct 16 '14 at 13:27
  • Toast request stacks which means they can show for a long period of time after app has been killed, not to mention left. – dominik4142 Oct 16 '14 at 13:28
  • @JaySnayder and dominik Your comments with standing on what not to do with a Toast. The issue really isnt the toast but the right place and way to cancel it when navigating through the app that is the issue. Persisting of the toast in my case has been done using a timertask, so killing the timertask end the toast. I am just having trouble finding the right way – James Wahome Oct 16 '14 at 13:33
  • Toast is Toast- thinking about modifying well i go for Length_short and check yourself before you fire your activity to sum it up fyi i didnt reali get you..so sorry if im not lucid to you – Elltz Oct 16 '14 at 13:42

1 Answers1

0

This is abusive usage of toast, which is not designed for such tasks. And from android 4.1 user even can disable notifications for application(and this also disables toasts for app) - so toast is unreliable and should only show additional inforamtion which can be ignored with no harm.

Your way is to use overlay activity. See How to create always-top fullscreen overlay activity in Android

Community
  • 1
  • 1
curioushikhov
  • 2,461
  • 2
  • 30
  • 44