5

I have a ViewPager with several fragments inside, and one of them contains a webview. Inside that WebView, I would like to open links to images (jpeg/png) in a designated activity which I use to show images:

MyFragment.java

    webView.setWebViewClient(new WebViewClient() {

        public void startImageView(String url) {
            Intent imageIntent = new Intent(getActivity(), ImageActivity.class);
            imageIntent.putExtra(ImageActivity.IMAGE_URL, Uri.parse(url));
            startActivity(imageIntent);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url){
            if (url.toLowerCase().endsWith(".png") || url.toLowerCase().endsWith(".jpg") || url.toLowerCase().endsWith(".jpeg")) {
                startImageView(url);
            } else {
                view.loadUrl(url);
            }
            return false;
       }

    });

ImageActivity.java

public class ImageActivity extends ActionBarActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_image_full_size);
...
}

AndroidManifest.xml

...
        <activity
            android:name="com.example.main.ImageActivity"
            android:label="Picture"
            android:screenOrientation="portrait"
        >
        </activity>
...

When I click on an image link inside the webview, the image activity indeed loads. However, the activity is loaded inside the fragment, instead of opening in a new window (as activities usually do). When I start this activity from a different activity (and not inside a webview as mentioned above) the activity opens in a new window as expected. Any ideas how to open the ImageActivity in a new window when started from WebView?

Sharath
  • 691
  • 8
  • 23
Ohad
  • 1,450
  • 4
  • 18
  • 27
  • I like your question. – M D Apr 28 '15 at 08:59
  • 1
    how can ImageActivity.class has class name as FullImageActivity? – Anirudh Sharma Apr 28 '15 at 08:59
  • @AnirudhSharma - sorry, you're right. I changed it for clarity in this post, and forgot to rename all occurrences. Fixed it. – Ohad Apr 28 '15 at 09:04
  • Did you try `getActivity().getApplicationContext()` without `getActivity()` in `startImageView`? – Wishmaster Apr 28 '15 at 09:24
  • @Wishmaster - yes, unfortunately it did not work. – Ohad Apr 28 '15 at 09:29
  • @Ohad can you explain what is mean "new window"? I've made sample like yours and `ImageActivity` opens standard. – Wishmaster Apr 28 '15 at 10:06
  • Try with JavaScript interface – Bojan Kseneman Apr 28 '15 at 11:01
  • http://stackoverflow.com/questions/29779954/android-webview-showtoast-onbuttonclick/29780068#29780068 – Bojan Kseneman Apr 28 '15 at 11:04
  • @Wishmaster The activity is opened inside the fragment. That is, the ViewPager is still visible, and the activity shows up inside of the current fragment. I would like the activity to open in a new window on top of the View Pager. – Ohad Apr 28 '15 at 12:02
  • Are you sure that the activity is opening inside the fragment? I believe its the web view rendering the image. Add some logcat and see that it passes the condition and actually executes startImageView() method. Also return appropriately from shouldUrlLoading() method. – km86 Jun 11 '15 at 12:29

1 Answers1

0

What you're saying is impossible on Android. startActivity would never render an Activity "inside of a fragment". Therefore once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth.

if (url.toLowerCase().endsWith(".png") || url.toLowerCase().endsWith(".jpg") || url.toLowerCase().endsWith(".jpeg")) {
                startImageView(url);
            } else {
                view.loadUrl(url);
            }

Your if statement is most likely going into the ELSE part and so the WebView is loading the image.

Do you have an example of a url for the images you load - perhaps add logs to this if else statement.

Do your images have query paramaters on them? i.e. is the url actually www.foo.com/bar/image.png?w=300h=300? Therefore endsWith will not work. Try changing to this: url.toLowerCase().contains(".png")

i.e.

String candidateUrl = url.toLowerCase();
if (candidateUrl.contains(".png") 
   || candidateUrl.contains(".jpg") 
   || candidateUrl.contains(".jpeg")) {
   Log.d("MyTag", "Start a new activity for: " + url);
   startImageView(url);
} else {
   Log.d("MyTag", "Load inside webview " + url);
   view.loadUrl(url);
}
Blundell
  • 75,855
  • 30
  • 208
  • 233