从使用导航组件的片段返回时会触发哪些方法?

时间:2019-09-17 14:37:32

标签: android android-fragments android-architecture-navigation

我有一个名为MainActivity的活动,其中包含public class Test { public static void main(String[] args) { Scanner scan = new Scanner (System.in); AddressBook ad1 = new AddressBook(); String firstName=""; String lastName=""; String key=""; int choice=0; do{ System.out.println("********************************************************************************"); System.out.println("Welcome to the Address book. Please pick from the options below.\n"); System.out.println("1.Add user \n2.Remove user \n3.Edit user \n4.List Contact \n5.Sort contacts \n6.Exit"); System.out.print("Please enter a choice: "); choice = scan.nextInt(); if(choice==1){ //Add user System.out.print("Please enter firstname: "); firstName=scan.next(); System.out.print("Please enter lastname: "); lastName=scan.next(); Address address = new Address(); key = lastName.concat(firstName); Person person = new Person(firstName,lastName); ad1.addContact(key,person); System.out.println("key: " + key); } else if(choice==2){ //Remove user System.out.println("Please enter name of user to remove: "); scan.useDelimiter(System.getProperty("line.separator")); key=scan.next(); System.out.println("name:" + key); ad1.removeContact(key); } else if(choice==3){ //Edit user } else if(choice==4){ //List contact ad1.listAllContacts(); } else if(choice==5){ //Sort contacts } }while(choice!=6); } } 。使用导航组件,我已经设置了可以从FirstFragment打开的单个片段是FirstFragment。我还启用了后退按钮,以便能够从SecondFragment导航到SecondFragment。问题是当我回到FirstFragment时,触发了FirstFragment方法。在这种情况下会触发什么方法?怎么知道我何时从onStart()回到SecondFragment

1 个答案:

答案 0 :(得分:1)

如果您正确执行此操作,则只要在第二个片段中按返回按钮,就会调用第一个片段的

onCreateView()方法。该片段的布局在onCreateView()方法中被放大。这样您将看到第一个片段屏幕。