繁体   English   中英

从Android中的两个不同活动打开时的不同活动行为

[英]Different Behaviors of Activity when Opened from two Different Activities in Android

在我问这个问题之前,我确实在StackOwerflow上看了好几天的不同答案,但是找不到答案。

这就是我正在做的-我有一个具有UserProfileActivity的应用程序,我希望能够从2个不同的活动(从myContactsListActivitymessageActivity 我要在UserProfileActivity中发送的数据包含userIduserNameprofilePhootoaboutUser 在第一种情况下,我想通过myContactsListActivity意图传递此数据,在第二种情况下,我仅要传递myContactsListActivity userId并进行调用以从服务器获取数据。

这就是我现在的做法。 myContactsListActivity打开它时,我使用意图将数据传递给UserProfileActivity ,并且仅传递messageActivity userId ,并使用if来确定调用的意图。

简而言之: 活动A可以从活动B和C中打开。我需要两种不同的行为。 如果从形式B打开,则所有内容都通过意图传递,如果从形式C,则仅传递userId并调用服务器。 我将如何确定从哪个活动开始,以及设置不同行为的最佳方法是什么

这是我的代码IT WORKS ,但是我对此不满意,我正在寻找更好的解决方案:

TextView textViewUserName, textViewAbout;
ImageView imageView;
Toolbar toolbar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_profile);

    Intent intent = getIntent();
    final String userId = intent.getStringExtra("userId");
    String userName = intent.getStringExtra("userName");
    String about = intent.getStringExtra("about");
    String image = intent.getStringExtra("image");

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    textViewUserName = (TextView) findViewById(R.id.contactUsername);
    textViewAbout = (TextView) findViewById(R.id.aboutMe);

    ColorGenerator generator = ColorGenerator.MATERIAL; 
    final int color = generator.getColor(userId);
    TextDrawable.IBuilder builder = TextDrawable.builder()
            .beginConfig()
            .withBorder(1)
            .endConfig()
            .rect();

    if (userName != null) {

        String firstLetter = intent.getStringExtra("userName").substring(0, 1);

        TextDrawable textDrawable = TextDrawable.builder().buildRect(firstLetter, color);
        imageView = (ImageView) findViewById(R.id.profile_image);
        imageView.setImageDrawable(textDrawable);
        Picasso.with(this)
                .load("http://192.168.0.13/mynewapp/profilephotos/" + image)
                .placeholder(textDrawable)
                .error(textDrawable)
                .centerCrop()
                .fit()
                .into(imageView);
        getSupportActionBar().setTitle(userName);

        Intent commentDescriptionIntent = new Intent(this, AboutFragment.class);
        commentDescriptionIntent.putExtra("userId", userId);
        commentDescriptionIntent.putExtra("userName", userName);
        commentDescriptionIntent.putExtra("about", about);
        setIntent(commentDescriptionIntent);

    } else {
        ApiInterface apiService =
                ApiClient.getClient().create(ApiInterface.class);

        Call<ContactResponse> call = apiService.userExists(userId);
        call.enqueue(new Callback<ContactResponse>() {
            @Override
            public void onResponse(Call<ContactResponse> call, retrofit2.Response<ContactResponse> response) {
                Contact contact = response.body().getResults();

                String firstLetter = contact.getUserName().substring(0, 1);
                TextDrawable textDrawable = TextDrawable.builder().buildRect(firstLetter, color);
                imageView = (ImageView) findViewById(R.id.profile_image);
                imageView.setImageDrawable(textDrawable);
                Picasso.with(getApplicationContext())
                        .load("http://localhost/mynewapp/profilephotos/" + contact.getThumbnailUrl())
                        .placeholder(textDrawable)
                        .error(textDrawable)
                        .centerCrop()
                        .fit()
                        .into(imageView);

                CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
                String userName = contact.getUserName();
                collapsingToolbarLayout.setTitle(userName);
            }

            @Override
            public void onFailure(Call<ContactResponse> call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
            }
        });
    }

尝试在Intent使用另一个extra值。

例如:

ActivityB

    Intent intent = new Intent(ActivityB.this, ActivityA.class);
    intent.putExtra("FROM_ACTIVITY", "B");
    // Others extra values
    startActivity(intent);

ActivityC

    Intent intent = new Intent(ActivityC.this, ActivityA.class);
    intent.putExtra("FROM_ACTIVITY", "C");
    // Others extra values
    startActivity(intent);

ActivityA执行此操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_profile);


    String fromActivity = getIntent().getStringExtra("FROM_ACTIVITY");

    if(fromActivity.equals("B")) {
        // Do something
    } else if(fromActivity.equals("C")) {
        // Do something
    }
}

希望这会有所帮助〜

这就是我继续这样做的方式。我们可以使用片段来加载活动,具体取决于活动的不同状态,

因此,您可以有2个不同的片段。 可能会加载相同的UI / xml视图,但是行为有所不同,只需设置来自意图的值即可。 和其他从外部api加载的东西。

注意:

尝试使用加载程序从外部api加载内容。 具有自己的回调,您可以在收到数据后用来加载数据。

这更多是一个高层次的想法,如果您还有其他问题,请告诉我

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM