I have an issue about activity lifecycle. I navigate between two trivial activities, A and B. A is the parent of B.
<activity
android:name=".BActivity"
android:label="#string/title_activity_b"
android:parentActivityName=".AActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.example.AActivity" />
</activity>
In the activity classes themself i do absolutely nothing, except overriding the lifecycle methods for debug purposes and starting the next activity.
Activity A:
public class A extends Activity {
int itemId;
#Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}
#Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart");
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
#Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
#Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
Log.d(TAG, " ------------- ");
Log.d(TAG, "onCreate");
itemId = getItemExampleItemId();
Button goToActivityB = (Button)findViewById(R.id.goto_activity_b);
goToActivityB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(AActivity.this, BActivity.class);
intent.putExtra("extraMessage", itemId);
startActivity(intent);
}
});
}
}
Activity B:
public class BActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
}
}
if I navigate from A to B and back, the logcat shows:
D/AActivity﹕ -------------
D/AActivity﹕ onCreate
D/AActivity﹕ onStart
D/AActivity﹕ onResume
D/AActivity﹕ onPause
D/AActivity﹕ onStop
D/AActivity﹕ onDestroy
D/AActivity﹕ -------------
D/AActivity﹕ onCreate
D/AActivity﹕ onStart
D/AActivity﹕ onResume
D/AActivity﹕ onPause
D/AActivity﹕ onStop
D/AActivity﹕ onDestroy
D/AActivity﹕ -------------
D/AActivity﹕ onCreate
D/AActivity﹕ onStart
D/AActivity﹕ onResume
D/AActivity﹕ onPause
D/AActivity﹕ onStop
D/AActivity﹕ onDestroy
D/AActivity﹕ -------------
D/AActivity﹕ onCreate
D/AActivity﹕ onStart
D/AActivity﹕ onResume
D/AActivity﹕ onPause
D/AActivity﹕ onStop
D/AActivity﹕ onDestroy
D/AActivity﹕ -------------
...
Has anybody an idea whats the problem?
You can for example achieve that by setting :
<activity
android:name=".ActivityA"
android:launchMode="singleTask" />
in your AndroidManifest.
For more Info look at the documentation
Related
protected void onCreate (Bundle savedInstancestate) {
super.onCreate(savedInstancestate);
addPreferencesFromResources(R.xml.prefs);
}
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
addPreferencesFromResource (R.xml.YourFileName);
}
How can i start an instance of activity.
My custom activity has a constructor take some paramters.
public class CustomActivity extends Activity {
public CustomActivity(String someParameters) {
}
}
And i create an instance of it. How to start it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomActivity activity = new CustomActivity("TEST");
// how to start to the above activity from this activity
}
I am unit testing activity with Robolectric framework and getting the following error of parse initialization error.
MyActivity.java
public class WelcomeActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// hide action bar
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.hide();
//Checking Current User
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null)
{
Intent i=new Intent(WelcomeActivity.this, ViewPagerStyleLandingActivity.class);
startActivity(i);
WelcomeActivity.this.finish();
}
setContentView(R.layout.actvity_welcome);
SharedPreferences pref = getSharedPreferences(Constants.SIGNUP_CHECK_PREF, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.PREF_KEY_FIRST_RUN, false);
editor.commit();
Button loginbut=(Button) findViewById(R.id.login);
Button registerbut=(Button) findViewById(R.id.register);
loginbut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i=new Intent(WelcomeActivity.this,LoginActivity.class);
startActivity(i);
}
});
registerbut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i=new Intent(WelcomeActivity.this,RegisterActivity.class);
startActivity(i);
}
});
}
}
and have made the test case as
#RunWith(RobolectricTestRunner.class)
public class WelcomeActivityTest {
private WelcomeActivity activity;
private Button loginbut;
#Before
public void setup() {
activity = Robolectric.buildActivity(WelcomeActivity.class).create().get();
loginbut = (Button) activity.findViewById(R.id.button);`}
#Test
public void checkActivityNotNull() throws Exception {
assertNotNull(activity);
}
#Test
public void shouldHaveButtonThatSaysAudit() throws Exception{
assertThat((String) loginbut.getText(), equalTo("Login"));
}
#Test
public void pressingLaunchButtonForSecondActivity() throws Exception {
String resultValue = "Testing Text";
assertNotNull(loginbut);
loginbut.performClick();
Intent startedIntent = shadowOf(activity).getNextStartedActivity();
assertThat(resultValue, equalTo(startedIntent.getStringExtra("result")));
}
private ShadowContextWrapper shadowOf(WelcomeActivity activity2) {
return null;
}
#After
public void tearDown() throws Exception {
// Nothing Here
}
}
now getting the error
when running with jUnit
I have layouts main activity and another list activity. But when i try to call list activity i am getting crash. Please help me to start list activity (Note: List activity has EditText and button to add text to existing listview in list Activity)
All i wanted to add text listview which is already exist in listactivity.
Many Thanks!!
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
EditText txt=(EditText)findViewById(R.id.txtPass);
if (txt.getText().length()==0){
Toast.makeText(this, "Please enter password to continue!!", Toast.LENGTH_SHORT).show();
txt.getText().clear();
return;
}
if(txt.getText().toString().contains("Test")){
Button btn;
btn=(Button)findViewById(R.id.btnSubmit);
Intent intent=new Intent(this,Passwordlist.class);
startActivity(intent);
}
}
First, make some change to your code:
Button btn;
EditText txt
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Do this stuff here and not in onClick. Your views should be defined in //onCreate() and before the onCLick()
txt=(EditText)findViewById(R.id.txtPass);
btn=(Button)findViewById(R.id.btnSubmit);
}
public void onClick(View view){
Intent intent=new Intent(YourActivity.this,Passwordlist.class);
if (txt.getText().toString().matches("")){
Toast.makeText(this, "Please enter password to continue!!", Toast.LENGTH_SHORT).show();
txt.getText().clear();
}
else if(txt.getText().toString().contains("Test")){
startActivity(intent);
}
}
See if this works.
I'm trying to get up to speed on using GWT Activities and Places. I'm testing with some source code originally found on this good blog post.
I'm finding the Handlers that get added during bind() never seem to removed. My little understanding of the Activity javadoc had me thinking they should get automagically removed by the time the Activity's onStop() method is invoked.
All event handlers it registered will have been removed before this
method is called.
But each time I click a button the corresponding handler is called n+1 times.
What am I missing? Please let me know if there is more info I can provide.
Here's a relevant snippet from the code:
public class ContactsActivity extends AbstractActivity {
private List<ContactDetails> contactDetails;
private final ContactsServiceAsync rpcService;
private final EventBus eventBus;
private final IContactsViewDisplay display;
private PlaceController placeController;
public interface IContactsViewDisplay {
HasClickHandlers getAddButton();
HasClickHandlers getDeleteButton();
HasClickHandlers getList();
void setData(List<String> data);
int getClickedRow(ClickEvent event);
List<Integer> getSelectedRows();
Widget asWidget();
}
public ContactsActivity(ClientFactory factory) {
GWT.log("ContactActivity: constructor");
this.rpcService = factory.getContactServiceRPC();
this.eventBus = factory.getEventBus();
this.display = factory.getContactsView();
this.placeController = factory.getPlaceController();
}
#Override
public void start(AcceptsOneWidget container, EventBus eventBus) {
GWT.log("ContactActivity: start()");
bind();
container.setWidget(display.asWidget());
fetchContactDetails();
}
public void bind() {
GWT.log("ContactActivity: bind()");
display.getAddButton().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
GWT.log("Add button clicked");
ContactsActivity.this.placeController.goTo(new NewContactPlace(""));
}
});
display.getDeleteButton().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
GWT.log("ContactActivity: Delete button clicked");
deleteSelectedContacts();
}
});
display.getList().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
GWT.log("ContactActivity: List clicked");
int selectedRow = display.getClickedRow(event);
if (selectedRow >= 0) {
String id = contactDetails.get(selectedRow).getId();
ContactsActivity.this.placeController.goTo(new EditContactPlace(id));
}
}
});
}
Events registered via. the EventBus passed to AbstractActivity#start() will be unregistered by the time onStop() is called. The event handlers registered in the above bind() method, however, are not registered via the EventBus and are not visible to the abstract base class. You need to unregister them yourself:
public class ContactsActivity extends AbstractActivity {
private List<HandlerRegistration> registrations = new ArrayList();
private void bind() {
registrations.add(display.getAddButton().
addClickHandler(new ClickHandler() { ... }));
registrations.add(display.getDeleteButton().
addClickHandler(new ClickHandler() { ... }));
registrations.add(display.getList().
addClickHandler(new ClickHandler() { ... }));
}
#Override
public void onStop() {
for (HandlerRegistration registration : registrations) {
registration.removeHandler();
}
registrations.clear();
}
}
I found it best to handle registration in the view - make it responsible for only keeping one click hander active for each button.
Instead of:
class View {
Button commitButton;
public HasClickHandlers getCommit () {return commitButton;}
}
..and link to this in the Activity:
view.getCommit.addClickHandler(new Clickhandler()...
Do this in the View:
class View {
private Button commitButton;
private HandlerRegistration commitRegistration = null;
public void setCommitHandler (ClickHandler c) {
commitRegistraion != null ? commitRegistration.removeRegistration ();
commitRegistration = commitButton.addClickHandler (c);
}
}
And the Activity:
view.setCommitHandler (new ClickHandler () ...
Hope that helps.