[英]How to display text in a new activity when two radio buttons from two different activities are clicked?
[英]Different Behaviors of Activity when Opened from two Different Activities in Android
在我問這個問題之前,我確實在StackOwerflow上看了好幾天的不同答案,但是找不到答案。
這就是我正在做的-我有一個具有UserProfileActivity
的應用程序,我希望能夠從2個不同的活動(從myContactsListActivity
和messageActivity
。 我要在UserProfileActivity
中發送的數據包含userId
, userName
, profilePhooto
和aboutUser
。 在第一種情況下,我想通過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.