我正在尝试创建活动,这些活动会指向主页并打开主页屏幕,“数据库现在已更新”。但是,我不知道如何在收到意图时抽出吐司。但我不知道如何很好地接受意图,这是我的代码。
public class MainActivity extends Activity {
TextView time;
Button viewStock, descriptionSearch;
String lastUpdateStandard = "Last Update Time: ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewStock = (Button) findViewById(R.id.detailSearchButton);
descriptionSearch = (Button) findViewById(R.id.descriptionSearchButton);
time = (TextView) findViewById(R.id.time);
// retrieve time from database to see last update time
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<String> lastUpdateTime = db.getLastUpdate();
time.setText(lastUpdateStandard + lastUpdateTime.get(0));
Intent receivedIntent = getIntent();
/**
*
* What should I write if there is no received Intent?
* Probably I need an if-else statement, but I have no idea how it should be made.
*/
}
答案 0 :(得分:0)
由于所有活动都是由意图启动的(Android是按照这种方式设计的),因此建议不要进行当前想要进行的检查,它们将始终返回意图。您应该做的是检查用于启动活动的意图是否具有您要提取的数据。你显然只是因为你需要某种意图正确的数据而需要这个意图吗?因此,不应检查是否返回了意图,而应检查您从意图中获得的额外内容。例如,如果你想用字符串&#34; hello&#34;来提取额外的内容。作为关键,你将使用:
Intent receivedIntent = getIntent();
if(receivedIntent.getStringExtra("hello") != null) {
// Means the intent has the data you expect
} else {
// It does not have the data you expect
// Do some processing here
}
如果您要检查是否收到意图,您将始终获得一个值