开发者

Android intent pass data to activity - am i doing this right?

开发者 https://www.devze.com 2023-03-19 07:22 出处:网络
Another beginner android question... I have an ImageView in an activity called introduction. I want to change the image in this ImageView depending a Fling gesture. The fling detection is working fin

Another beginner android question...

I have an ImageView in an activity called introduction. I want to change the image in this ImageView depending a Fling gesture. The fling detection is working fine, I have

public class FlingGestureListener extends GestureDetector.SimpleOnGestureListener

Which calls

private void RaiseIntent(String direction)
    {
        Intent intent = new Intent(DemoApp.getAppContext(), Introduction.class);
        intent.putExtra("direction", direction);
        String category =   DemoApp.getAppContext().getStr开发者_如何学Cing(R.string.introduction_imageview_fling);
        intent.addCategory(category);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        DemoApp.getAppContext().startActivity(intent);
    }

It is primitive but the string "left" or "right" is passed to RaiseIntent on a Fling gesture.

Introduction is the activity in which the ImageView sits in its view.

In the app manifest for this activity i have:

<activity android:name=".Activities.Introduction">
        <intent-filter>
              <category android:name="@string/introduction_imageview_fling"/>
        </intent-filter>
    </activity>

So, in the introduction activity I can now, on resume, call

getIntent

And pull out the direction from the putExtra. Life is good, I can rely on the value passed back to tell me which image to load into the ImageView next.

My problem is, and my lack of android knowledge may be showing here, is this feels dirty.

Im going to have to do some checks on the intent itself on OnResume to make sure the category is @string/introduction_imageview_fling, and then pull out the data. I know onResume will be called over onCreate as the gesture is rasied from inside of the activity after onCreate has been called. But, again, it feels dirty to me! Is this really the best way to pass data from a SimpleOnGestureListener back to an activity? Am i missing something fundamental?

Thanks


My follow up to this was far too long for comments, and it does contain and answer:

Ok, so i solved this but only by nesting both the

ImageViewOnTouchListener implements View.OnTouchListener

and

FlingGestureListener extends GestureDetector.SimpleOnGestureListener

within the same activity. This then allowed me to change the Intent flag mode to Intent.FLAG_ACTIVITY_SINGLE_TOP and wire up

public void onNewIntent(Intent intent)
{

}

and just call

startActivity(intent);

as apposed to

DemoApp.getAppContext().startActivity(intent);

Where DemoApp is the Application file and context a singleton. Ok this is fine, it works as i expect now and life is good!

But, I have nested classes and could, as far as my novice mind can tell, end up violating DRY. When trying to call FLAG_ACTIVITY_SINGLE_TOP from a separate class I was told by error log that only the

FLAG_ACTIVITY_NEW_TASK

Can be called from outside the activity, and that makes sense in most cases as the activity might have been nuked off the stack, or never started at all. However in my example here, where this is all one activity, just separate class files, is this really the only way to solve this? Or am i looking at this from completely the wrong angle?

Thanks again.

0

精彩评论

暂无评论...
验证码 换一张
取 消